By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,441 Members | 1,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,441 IT Pros & Developers. It's quick & easy.

php equivalent to perl's || behavior?

P: n/a
one feature of perl I'm desparately missing in php is using || to assign
the first non-empty value in a list to a variable. For example,

# perl example 1
$a = 1;
$b = 2;
$c = $a || $b;
print $c; # displays 1

# perl example 2
$a = 1;
$b = 2;
$c = $d || $b;
print $c; # displays 2, since $d is empty

# perl example 3
$a = 'apple';
$b = 'banana';
$c = 'cherry';
$d = $a || $b || $c;
print $d; # displays 'apple';

# perl example 4
$a = '';
$b = '';
$c = 'cherry';
$d = $a || $b || $c;
print $d; # displays 'cherry';

This is such a handy language construct. Is there any such php
equivalent that accomplishes this? I've already written a function to do
it. (Like this: $d = value($a, $b, $c). But making value() visible
everywhere is something I'd rather avoid if there's something built-in
to php.)

--cd
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"Coder Droid" <co********@likethiswouldstopspam.hotmail.com> wrote in
message news:Ps*******************@fe2.texas.rr.com...
one feature of perl I'm desparately missing in php is using || to assign
the first non-empty value in a list to a variable. For example,

# perl example 1
$a = 1;
$b = 2;
$c = $a || $b;
print $c; # displays 1

# perl example 2
$a = 1;
$b = 2;
$c = $d || $b;
print $c; # displays 2, since $d is empty

# perl example 3
$a = 'apple';
$b = 'banana';
$c = 'cherry';
$d = $a || $b || $c;
print $d; # displays 'apple';

# perl example 4
$a = '';
$b = '';
$c = 'cherry';
$d = $a || $b || $c;
print $d; # displays 'cherry';

This is such a handy language construct. Is there any such php
equivalent that accomplishes this? I've already written a function to do
it. (Like this: $d = value($a, $b, $c). But making value() visible
everywhere is something I'd rather avoid if there's something built-in
to php.)

--cd

No, unfortunately.

Jul 17 '05 #2

P: n/a
"Coder Droid" <co********@likethiswouldstopspam.hotmail.com> wrote in
message news:Ps*******************@fe2.texas.rr.com...
-snip-
# perl example 4
$a = '';
$b = '';
$c = 'cherry';
$d = $a || $b || $c;
print $d; # displays 'cherry';

This is such a handy language construct. Is there any such php
equivalent that accomplishes this? I've already written a function to do
it. (Like this: $d = value($a, $b, $c). But making value() visible
everywhere is something I'd rather avoid if there's something built-in
to php.)


The closest that php comes is the Ternary Operator

http://uk.php.net/operators.comparison

Jul 17 '05 #3

P: n/a
> > This is such a handy language construct. Is there any such php
equivalent that accomplishes this? I've already written a function to do it. (Like this: $d = value($a, $b, $c). But making value() visible
everywhere is something I'd rather avoid if there's something built-in to php.)

--cd


No, unfortunately.


Hmmm... that isn't the answer I wanted. ;) But, it's the one I
expected. I thought I'd pretty much exhausted my search for something.

The function I wrote to do this is:

function value()
{
$numargs = func_num_args();
for ($i = 0; $i < $numargs; $i++) {
$value = func_get_arg($i);
if ($value != '') {
return $value;
}
}
}

Which obviously loops though all values and returns the first non-empty
one. I use this to determine the value for something when the source
value might come from one of a number of places. e.g., let's say I'm
trying to set the default language, I might use it like this:

$a = value($_REQUEST['lang'], $config['lang'], $_ENV['lang'], 'en');

This allows a number of fallback values. While it works, it is about 200
bytes, which is a hundred times larger than "||". And since it's a
function, I've got to worry about its visibility everywhere. Right now,
it's a method of an object.

Hmmmm.... well, okay. I'm done thinking out loud. Guess I'll ponder the
best way to do this some more.

--cd
Jul 17 '05 #4

P: n/a
On Tue, 12 Oct 2004 13:09:13 GMT, Coder Droid wrote:
The function I wrote to do this is:

function value()
{
$numargs = func_num_args();
for ($i = 0; $i < $numargs; $i++) {
$value = func_get_arg($i);
if ($value != '') {
return $value;
}
}
}

Which obviously loops though all values and returns the first non-empty
one. I use this to determine the value for something when the source
value might come from one of a number of places. e.g., let's say I'm
trying to set the default language, I might use it like this:

$a = value($_REQUEST['lang'], $config['lang'], $_ENV['lang'], 'en');

This allows a number of fallback values. While it works, it is about 200
bytes, which is a hundred times larger than "||". And since it's a
function, I've got to worry about its visibility everywhere. Right now,
it's a method of an object.


This might be a bit smaller:

function value() {
foreach(func_get_args() as $a) {
if($a != '') return $a;
}
}

Berislav
Jul 17 '05 #5

P: n/a
> function value() {
foreach(func_get_args() as $a) {
if($a != '') return $a;
}
}


True. And that does look cleaner, so I'll probably use that. But it's
still a function, so I still have my visibility problems. I just hate
having to use "global $whatever" every time I want to declare this
object to access this method.

So I'm thinking about (just this once) having one function defined
outside of all my objects (a "superglobal function" of sorts) and
declaring it at the beginning. Hmmmm... I might give it a shot and see
how it turns out. Sometimes it's hard to tell without a test drive.

--cd
Jul 17 '05 #6

P: n/a
On Tue, 12 Oct 2004 20:37:25 GMT, Coder Droid wrote:
True. And that does look cleaner, so I'll probably use that. But it's
still a function, so I still have my visibility problems. I just hate
having to use "global $whatever" every time I want to declare this
object to access this method.

So I'm thinking about (just this once) having one function defined
outside of all my objects (a "superglobal function" of sorts) and
declaring it at the beginning.


Which is a completely legal approach in PHP.

Berislav
Jul 17 '05 #7

P: n/a
Coder Droid wrote:
function value() {
foreach(func_get_args() as $a) {
if($a != '') return $a;
}
}


True. And that does look cleaner, so I'll probably use that. But it's
still a function, so I still have my visibility problems. I just hate
having to use "global $whatever" every time I want to declare this
object to access this method.

So I'm thinking about (just this once) having one function defined
outside of all my objects (a "superglobal function" of sorts) and
declaring it at the beginning. Hmmmm... I might give it a shot and see
how it turns out. Sometimes it's hard to tell without a test drive.


I use a class which works as a container for all my functions I want to see
globally, and let all other classes inherit it; so that kind of function is
visible everywhere.

class functions
{
function value()
{
[...]
}
}

class otherclass extends functions
{
function use_value($a, $b, $c)
{
return $this->value($a, $b, $c);
}
}

And also in the calling script you can use $otherclass->value() as soon as
you have an instance of otherclass.

HTH
Markus
Jul 17 '05 #8

P: n/a
> > So I'm thinking about (just this once) having one function defined
outside of all my objects (a "superglobal function" of sorts) and
declaring it at the beginning.


Which is a completely legal approach in PHP.


And which is what I ended up doing. It's not too bad, actually.

--cd
Jul 17 '05 #9

P: n/a
> I use a class which works as a container for all my functions I want
to see
globally, and let all other classes inherit it; so that kind of function is visible everywhere.


Hmmm... hadn't thought of trying that either. Thanks for the tip.

--cd
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.