473,396 Members | 2,052 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.

php equivalent to perl's || behavior?

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
9 1724

"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
"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
> > 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
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
> 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
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
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
> > 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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Edward WIJAYA | last post by:
Hi, I am new to Python, and I like to learn more about it. Since I am used to Perl before, I would like to know what is Python equivalent of Perl code below: $filename = $ARGV;
3
by: Caj Zell | last post by:
Hello, I am looking a little bit at python and am curious of one thing I often use in perl. Suppose, I want to change the string "best_composer" to "Zappa" in a document called facts.txt, then...
5
by: mbbx6spp | last post by:
Hi All, I already searched this newsgroup and google groups to see if I could find a Python equivalent to Perl's Template::Extract, but didn't find anything leading to a Python module that had...
4
by: Susan Baker | last post by:
Hi, I'm working on s simple parser and I would like to split up multi-line expressions into single expressions. Is there a way whereby I could slurp the lines into an array (or other container)?...
2
by: Greger | last post by:
Hi is there any equivalent of perl's use strict; in PHP,?? TIA -- http://www.gregerhaga.net
12
by: rurpy | last post by:
Is there an effcient way (more so than cgi) of using Python with Microsoft IIS? Something equivalent to Perl-ISAPI?
35
by: lnatz | last post by:
Hi, Is there an equivalent to the perl command chomp in C? And if there is no exact equivalent command, how would I go about removing the "\n" at the end of a stdin? Thank you, Natalie
3
by: bwooster47 | last post by:
Following python code prints out incorrect UTC Offset - the python docs say that %z is not fully supported on all platforms - but on Linux Fedora FC5, perl code works and python does not - is this...
0
by: Gabriel Genellina | last post by:
En Thu, 08 May 2008 09:11:56 -0300, Michael Mabin <d3vvnull@gmail.comescribió: Like this? <code> import sys,fileinput for line in fileinput.input(inplace=True):...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.