Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ternary operator or if() statement for default value?

0 views
Skip to first unread message

law...@gmail.com

unread,
Oct 9, 2008, 3:20:29 PM10/9/08
to
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?

Michael Fesser

unread,
Oct 9, 2008, 3:58:47 PM10/9/08
to
.oO(law...@gmail.com)

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

law...@gmail.com

unread,
Oct 9, 2008, 4:06:41 PM10/9/08
to
On Oct 9, 3:58 pm, Michael Fesser <neti...@gmx.de> wrote:

> 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!


Michael Fesser

unread,
Oct 9, 2008, 4:18:42 PM10/9/08
to
.oO(law...@gmail.com)

You're welcome.

Micha

Stewart Robert Hinsley

unread,
Oct 9, 2008, 4:33:16 PM10/9/08
to
In message
<9744b18b-8016-447b...@u46g2000hsc.googlegroups.com>,
law...@gmail.com writes

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

law...@gmail.com

unread,
Oct 10, 2008, 10:29:02 AM10/10/08
to
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?

Stewart Robert Hinsley

unread,
Oct 10, 2008, 10:49:17 AM10/10/08
to
In message
<b03b781b-c5da-4a94...@l33g2000pri.googlegroups.com>,
law...@gmail.com writes

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

Michael Fesser

unread,
Oct 10, 2008, 2:21:03 PM10/10/08
to
.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

Jerry Stuckle

unread,
Oct 10, 2008, 3:08:56 PM10/10/08
to

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
==================

Stewart Robert Hinsley

unread,
Oct 10, 2008, 4:34:36 PM10/10/08
to
In message <gco9br$e9o$1...@registered.motzarella.org>, Jerry Stuckle
<jstu...@attglobal.net> writes

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

0 new messages