Variable assignment using OR operator?

This is a discussion on Variable assignment using OR operator? within the PHP Language forums, part of the PHP Programming Forums category; Hello, I would like to set a variable to a default if 2 other vars are undef. Obviously there are ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-22-2005
jerrygarciuh
 
Posts: n/a
Default Variable assignment using OR operator?

Hello,

I would like to set a variable to a default if 2 other vars are undef.
Obviously there are several ways to do this but I would like to do it as I
would in Perl:

$memberState = $_POST['ms'] || $_GET['ms'] || 'Unapproved';

Is there acorrect way to do this in 1 line in PHP?

TIA,

jg


Reply With Quote
  #2 (permalink)  
Old 02-22-2005
Michael Fesser
 
Posts: n/a
Default Re: Variable assignment using OR operator?

.oO(jerrygarciuh)

>I would like to set a variable to a default if 2 other vars are undef.
>Obviously there are several ways to do this but I would like to do it as I
>would in Perl:
>
>$memberState = $_POST['ms'] || $_GET['ms'] || 'Unapproved';
>
>Is there acorrect way to do this in 1 line in PHP?


$memberState = isset($_REQUEST['ms']) ? $_REQUEST['ms'] : 'Unapproved';

Try if that fits your needs ($_REQUEST contains both POST and GET
variables).

Micha
Reply With Quote
  #3 (permalink)  
Old 02-22-2005
jerrygarciuh
 
Posts: n/a
Default Re: Variable assignment using OR operator?

Thanks!

jg


"Michael Fesser" <netizen@gmx.net> wrote in message
news:8v6n11d2eu33jicglqugge5ngejrrrsiqn@4ax.com...
> .oO(jerrygarciuh)
>
>>I would like to set a variable to a default if 2 other vars are undef.
>>Obviously there are several ways to do this but I would like to do it as I
>>would in Perl:
>>
>>$memberState = $_POST['ms'] || $_GET['ms'] || 'Unapproved';
>>
>>Is there acorrect way to do this in 1 line in PHP?

>
> $memberState = isset($_REQUEST['ms']) ? $_REQUEST['ms'] : 'Unapproved';
>
> Try if that fits your needs ($_REQUEST contains both POST and GET
> variables).
>
> Micha



Reply With Quote
  #4 (permalink)  
Old 02-24-2005
Matt Mitchell
 
Posts: n/a
Default Re: Variable assignment using OR operator?

"jerrygarciuh" <designs@no.spam.nolaflash.com> wrote in message
news:QPMSd.4029$SF.1615@lakeread08...
: Hello,
:
: I would like to set a variable to a default if 2 other vars are undef.
: Obviously there are several ways to do this but I would like to do it as I
: would in Perl:
:
: $memberState = $_POST['ms'] || $_GET['ms'] || 'Unapproved';
:
: Is there acorrect way to do this in 1 line in PHP?
:

I believe it would work just like that, you might want brackets though:

$memberState = ($_POST['ms'] || $_GET['ms'] || 'Unapproved');

otherwise, you'll get

if (!($memberState = $_POST['ms']))
{
$GET['ms'] or 'Unapproved';
}
which is probably not what you want


Reply With Quote
  #5 (permalink)  
Old 02-25-2005
jerrygarciuh
 
Posts: n/a
Default Re: Variable assignment using OR operator?

Actually that performs as though it is a conditional:

echo $memberState = ($x || $y || 'Unapproved');

outputs 1

Surely there is a construct for this in PHP? Michaels solution, while
helpful in that case does not solve the over all problem of an elegant 1
liner which does this lovely trick.

Thanks for the reply!

jg



"Matt Mitchell" <m_a_t_t_remove_the_underscores@metalsponge.net> wrote in
message news:b1qTd.175304$B8.158991@fe3.news.blueyonder.co .uk...
> "jerrygarciuh" <designs@no.spam.nolaflash.com> wrote in message
> news:QPMSd.4029$SF.1615@lakeread08...
> : Hello,
> :
> : I would like to set a variable to a default if 2 other vars are undef.
> : Obviously there are several ways to do this but I would like to do it as
> I
> : would in Perl:
> :
> : $memberState = $_POST['ms'] || $_GET['ms'] || 'Unapproved';
> :
> : Is there acorrect way to do this in 1 line in PHP?
> :
>
> I believe it would work just like that, you might want brackets though:
>
> $memberState = ($_POST['ms'] || $_GET['ms'] || 'Unapproved');
>
> otherwise, you'll get
>
> if (!($memberState = $_POST['ms']))
> {
> $GET['ms'] or 'Unapproved';
> }
> which is probably not what you want
>
>



Reply With Quote
  #6 (permalink)  
Old 02-25-2005
Jan Pieter Kunst
 
Posts: n/a
Default Re: Variable assignment using OR operator?

jerrygarciuh wrote:

> an elegant 1
> liner which does this lovely trick.


This is just my personal opinion, but since I started writing large
programs that have to be maintainable and understandable to myself and
others, I have really come to dislike 'elegant one liners'.

If I understood your problem correctly, I would simply write this:
(combining $_POST and $_GET in S_REQUEST):


if (isset($_REQUEST['ms']))
$memberState = $_REQUEST['ms'];
else
$memberState = 'Unapproved';


I'd rather have four lines which are immediately obvious than one line
which is a puzzle, however elegant it may be.

But to each his own, YMMV, etc.

JP

--
Sorry, <devnull@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:37 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0