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

ternary operator or if() statement for default value?

P: n/a
I'm trying to write some simple code, but I might be simplifying it
too much.

I have a function that either returns a string that I want, or FALSE.
In the code that I'm working on, I would like to default to another
string if the function returns false.

Here's the long way to do it:

$string = myFunction(2);
if ( $string === FALSE ) {
$string = "default value";

}

I would like to use the value from the function, if it's not false,
else the default value.

if ( $string = myFunction(2) === FALSE ) {
$string = "default value";

}

If I were using the ternary operator to do this, I would do

$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;

but it looks like I'm calling the function twice, which would be
inefficient.

What's the most efficient way to do this?
Oct 9 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
..oO(la*****@gmail.com)
>I'm trying to write some simple code, but I might be simplifying it
too much.

I have a function that either returns a string that I want, or FALSE.
In the code that I'm working on, I would like to default to another
string if the function returns false.

Here's the long way to do it:

$string = myFunction(2);
if ( $string === FALSE ) {
$string = "default value";

}

I would like to use the value from the function, if it's not false,
else the default value.

if ( $string = myFunction(2) === FALSE ) {
$string = "default value";

}

If I were using the ternary operator to do this, I would do

$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;

but it looks like I'm calling the function twice, which would be
inefficient.

What's the most efficient way to do this?
Efficiency is not really an issue. Question is what's more convenient
and what you feel more comfortable with. You could do something like

$string = ($value = myFunction(2)) === FALSE
? 'default value'
: $value;

But I would probably use a little helper function with two arguments:

function strInit($value, $default)
return ($value !== FALSE) ? $value : $default;
}

And then:

$string = strInit(myFunction(2), 'default value');

Micha
Oct 9 '08 #2

P: n/a
On Oct 9, 3:58*pm, Michael Fesser <neti...@gmx.dewrote:
Efficiency is not really an issue. Question is what's more convenient
and what you feel more comfortable with. You could do something like

$string = ($value = myFunction(2)) === FALSE
* ? 'default value'
* : $value;
Thanks, Micha! That's exactly what I was looking for!
Oct 9 '08 #3

P: n/a
..oO(la*****@gmail.com)
>On Oct 9, 3:58*pm, Michael Fesser <neti...@gmx.dewrote:
>Efficiency is not really an issue. Question is what's more convenient
and what you feel more comfortable with. You could do something like

$string = ($value = myFunction(2)) === FALSE
* ? 'default value'
* : $value;

Thanks, Micha! That's exactly what I was looking for!
You're welcome.

Micha
Oct 9 '08 #4

P: n/a
In message
<97**********************************@u46g2000hsc. googlegroups.com>,
la*****@gmail.com writes
>I'm trying to write some simple code, but I might be simplifying it
too much.

I have a function that either returns a string that I want, or FALSE.
In the code that I'm working on, I would like to default to another
string if the function returns false.

Here's the long way to do it:

$string = myFunction(2);
if ( $string === FALSE ) {
$string = "default value";

}

I would like to use the value from the function, if it's not false,
else the default value.

if ( $string = myFunction(2) === FALSE ) {
$string = "default value";

}

If I were using the ternary operator to do this, I would do

$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;

but it looks like I'm calling the function twice, which would be
inefficient.
It's also problematic if the function has side effects.
>
What's the most efficient way to do this?
Checking the manual, it appears that PHP has inherited C-like assignment
operator semantics, so

$string = ($temp = myFunction(2)) === FALSE ? "default value" : $temp;

--
Stewart Robert Hinsley
Oct 9 '08 #5

P: n/a
On Oct 9, 4:33*pm, Stewart Robert Hinsley <{$new...@meden.demon.co.uk>
wrote:
In message
<9744b18b-8016-447b-9b81-a982f1a22...@u46g2000hsc.googlegroups.com>,
lawp...@gmail.com writes
>
If I were using the ternary operator to do this, I would do
$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;
but it looks like I'm calling the function twice, which would be
inefficient.

It's also problematic if the function has side effects.
"Side effects" being if the function returns anything other than a
valid string or false?

Oct 10 '08 #6

P: n/a
In message
<b0**********************************@l33g2000pri. googlegroups.com>,
la*****@gmail.com writes
>On Oct 9, 4:33*pm, Stewart Robert Hinsley <{$new...@meden.demon.co.uk>
wrote:
>In message
<9744b18b-8016-447b-9b81-a982f1a22...@u46g2000hsc.googlegroups.com>,
lawp...@gmail.com writes
>>
>If I were using the ternary operator to do this, I would do
>$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;
>but it looks like I'm calling the function twice, which would be
inefficient.

It's also problematic if the function has side effects.

"Side effects" being if the function returns anything other than a
valid string or false?
Side effects being if the function changes any non-local or static
variable during its execution.

So it the function has internal state, and this changes when it is
called, that's a side effect. (For example a random number generator
function.)

So if the function changes any global variable (or class variables, if
it's a member function of a class) that's a side effect.

So if a function changes a variable passed to it as a reference, that's
a side effect.

Pascal distinguishes between procedures (which can have side effects)
and functions (which can't), but most languages only have a single
construct, which covers both concepts. (Some languages call them
functions, some procedures, some subroutines, and I wouldn't be
surprised if there were other alternate names.)
--
Stewart Robert Hinsley
Oct 10 '08 #7

P: n/a
..oO(Stewart Robert Hinsley)
>Side effects being if the function changes any non-local or static
variable during its execution.

So it the function has internal state, and this changes when it is
called, that's a side effect. (For example a random number generator
function.)

So if the function changes any global variable (or class variables, if
it's a member function of a class) that's a side effect.

So if a function changes a variable passed to it as a reference, that's
a side effect.

Pascal distinguishes between procedures (which can have side effects)
and functions (which can't),
Functions in Pascal can have side effects as well. The only difference
is that they have a return value, while procedures have not.

Micha
Oct 10 '08 #8

P: n/a
Stewart Robert Hinsley wrote:
In message
<b0**********************************@l33g2000pri. googlegroups.com>,
la*****@gmail.com writes
>On Oct 9, 4:33 pm, Stewart Robert Hinsley <{$new...@meden.demon.co.uk>
wrote:
>>In message
<9744b18b-8016-447b-9b81-a982f1a22...@u46g2000hsc.googlegroups.com>,
lawp...@gmail.com writes
>>>
If I were using the ternary operator to do this, I would do

$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;

but it looks like I'm calling the function twice, which would be
inefficient.

It's also problematic if the function has side effects.

"Side effects" being if the function returns anything other than a
valid string or false?

Side effects being if the function changes any non-local or static
variable during its execution.

So it the function has internal state, and this changes when it is
called, that's a side effect. (For example a random number generator
function.)

So if the function changes any global variable (or class variables, if
it's a member function of a class) that's a side effect.

So if a function changes a variable passed to it as a reference, that's
a side effect.

Pascal distinguishes between procedures (which can have side effects)
and functions (which can't), but most languages only have a single
construct, which covers both concepts. (Some languages call them
functions, some procedures, some subroutines, and I wouldn't be
surprised if there were other alternate names.)
Not quite true. In Pascal, a procedure cannot return a value, while a
function must return a value.

Either one can have side effects.

As for "most languages only have a single construct" - true of many
languages which are based at least in part on C. Other languages such
as FORTRAN, PL/1 and others have both concepts. In those which don't
distinguish between the two, the presence of a return value is optional.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 10 '08 #9

P: n/a
In message <gc**********@registered.motzarella.org>, Jerry Stuckle
<js*******@attglobal.netwrites
>Stewart Robert Hinsley wrote:
>In message
<b0**********************************@l33g2000pr i.googlegroups.com>,
la*****@gmail.com writes
>>On Oct 9, 4:33 pm, Stewart Robert Hinsley <{$new...@meden.demon.co.uk>
wrote:
In message
<9744b18b-8016-447b-9b81-a982f1a22...@u46g2000hsc.googlegroups.com>,
lawp...@gmail.com writes
If I were using the ternary operator to do this, I would do

$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;

but it looks like I'm calling the function twice, which would be
inefficient.

It's also problematic if the function has side effects.

"Side effects" being if the function returns anything other than a
valid string or false?
Side effects being if the function changes any non-local or static
variable during its execution.
So it the function has internal state, and this changes when it is
called, that's a side effect. (For example a random number generator
function.)
So if the function changes any global variable (or class variables,
if it's a member function of a class) that's a side effect.
So if a function changes a variable passed to it as a reference,
that's a side effect.
Pascal distinguishes between procedures (which can have side
effects) and functions (which can't), but most languages only have a
single construct, which covers both concepts. (Some languages call
them functions, some procedures, some subroutines, and I wouldn't be
surprised if there were other alternate names.)

Not quite true. In Pascal, a procedure cannot return a value, while a
function must return a value.
Yes - I shouldn't have relied on my memory from the 25 years or more
since I used Pascal. Jensen and Wirth wrote "strongly discouraged".
>
Either one can have side effects.

As for "most languages only have a single construct" - true of many
languages which are based at least in part on C. Other languages such
as FORTRAN, PL/1 and others have both concepts. In those which don't
distinguish between the two, the presence of a return value is optional.
--
Stewart Robert Hinsley
Oct 10 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.