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?
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?
Comment