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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
.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 |
|
|||
|
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 |
|
|||
|
"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 |
|
|||
|
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 > > |
|
|||
|
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. |