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
> 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!
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
>
> >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
>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
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.
jstu...@attglobal.net
==================
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