473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jul 17 '05 #1
5 1852
.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
Jul 17 '05 #2
Thanks!

jg
"Michael Fesser" <ne*****@gmx.net> wrote in message
news:8v********************************@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

Jul 17 '05 #3
"jerrygarciuh" <de*****@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
Jul 17 '05 #4
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_****************************@metalsponge.net> wrote in
message news:b1********************@fe3.news.blueyonder.co .uk...
"jerrygarciuh" <de*****@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

Jul 17 '05 #5
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, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
9
by: Matthew Polder | last post by:
Hi, When a class Apple is written and the assignment operator is not explicitly declared, the operator Apple& operator=(const Apple&) is created by the compiler. Is there any difference...
15
by: Robert Sturzenegger | last post by:
// Code sequence A for (int i = 0; i < 10; ++i) { int k = something(); // some more code which uses k } // Code sequence B int k; for (int i = 0; i < 10; ++i) { k = something();
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
14
by: sathya_me | last post by:
Dear clc, I have a variable void *a; Since variable "a" can be assigned (point to) any type and also any type can be assigned to "a" (i.e means "a" = any typed variable; any typed variable =...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
2
by: Eric Lilja | last post by:
I'm looking at an assignment where the students are expected to write a class with a given purpose. According to the assignment, you should be able to do this with instances of the class:...
4
by: siddhu | last post by:
If there is reference member variable in the class, why doesn't default assignment operator work? class A { int& i; public: A( int& ii):i(ii){} //A& operator=(const A& a){i = a.i;} };
1
by: yccheok | last post by:
Hello all, I am confused on the correct assignment operator that should be implemented on the derived class. I refer to the document found at :- ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.