Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 9th, 2008, 08:25 PM
lawpoop@gmail.com
Guest
 
Posts: n/a
Default ternary operator or if() statement for default value?

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?
  #2  
Old October 9th, 2008, 09:05 PM
Michael Fesser
Guest
 
Posts: n/a
Default Re: ternary operator or if() statement for default value?

..oO(lawpoop@gmail.com)
Quote:
>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
  #3  
Old October 9th, 2008, 09:15 PM
lawpoop@gmail.com
Guest
 
Posts: n/a
Default Re: ternary operator or if() statement for default value?

On Oct 9, 3:58*pm, Michael Fesser <neti...@gmx.dewrote:
Quote:
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!


  #4  
Old October 9th, 2008, 09:25 PM
Michael Fesser
Guest
 
Posts: n/a
Default Re: ternary operator or if() statement for default value?

..oO(lawpoop@gmail.com)
Quote:
>On Oct 9, 3:58*pm, Michael Fesser <neti...@gmx.dewrote:
>
Quote:
>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
  #5  
Old October 9th, 2008, 09:45 PM
Stewart Robert Hinsley
Guest
 
Posts: n/a
Default Re: ternary operator or if() statement for default value?

In message
<9744b18b-8016-447b-9b81-a982f1a22edb@u46g2000hsc.googlegroups.com>,
lawpoop@gmail.com writes
Quote:
>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.
Quote:
>
>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
  #6  
Old October 10th, 2008, 03:35 PM
lawpoop@gmail.com
Guest
 
Posts: n/a
Default Re: ternary operator or if() statement for default value?

On Oct 9, 4:33*pm, Stewart Robert Hinsley <{$new...@meden.demon.co.uk>
wrote:
Quote:
In message
<9744b18b-8016-447b-9b81-a982f1a22...@u46g2000hsc.googlegroups.com>,
lawp...@gmail.com writes
Quote:
>
Quote:
If I were using the ternary operator to do this, I would do
>
Quote:
$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;
>
Quote:
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?

  #7  
Old October 10th, 2008, 04:05 PM
Stewart Robert Hinsley
Guest
 
Posts: n/a
Default Re: ternary operator or if() statement for default value?

In message
<b03b781b-c5da-4a94-9ffb-5633b2dc21fd@l33g2000pri.googlegroups.com>,
lawpoop@gmail.com writes
Quote:
>On Oct 9, 4:33*pm, Stewart Robert Hinsley <{$new...@meden.demon.co.uk>
>wrote:
Quote:
>In message
><9744b18b-8016-447b-9b81-a982f1a22...@u46g2000hsc.googlegroups.com>,
>lawp...@gmail.com writes
>
Quote:
>>
Quote:
>If I were using the ternary operator to do this, I would do
>>
Quote:
>$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;
>>
Quote:
>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
  #8  
Old October 10th, 2008, 07:25 PM
Michael Fesser
Guest
 
Posts: n/a
Default Re: ternary operator or if() statement for default value?

..oO(Stewart Robert Hinsley)
Quote:
>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
  #9  
Old October 10th, 2008, 08:15 PM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: ternary operator or if() statement for default value?

Stewart Robert Hinsley wrote:
Quote:
In message
<b03b781b-c5da-4a94-9ffb-5633b2dc21fd@l33g2000pri.googlegroups.com>,
lawpoop@gmail.com writes
Quote:
>On Oct 9, 4:33 pm, Stewart Robert Hinsley <{$new...@meden.demon.co.uk>
>wrote:
Quote:
>>In message
>><9744b18b-8016-447b-9b81-a982f1a22...@u46g2000hsc.googlegroups.com>,
>>lawp...@gmail.com writes
>>
Quote:
>>>
>>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.
jstucklex@attglobal.net
==================

  #10  
Old October 10th, 2008, 09:55 PM
Stewart Robert Hinsley
Guest
 
Posts: n/a
Default Re: ternary operator or if() statement for default value?

In message <gco9br$e9o$1@registered.motzarella.org>, Jerry Stuckle
<jstucklex@attglobal.netwrites
Quote:
>Stewart Robert Hinsley wrote:
Quote:
>In message
>><b03b781b-c5da-4a94-9ffb-5633b2dc21fd@l33g2000pri.googlegroups.com>,
>>lawpoop@gmail.com writes
Quote:
>>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".
Quote:
>
>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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles