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

if an multiple statements

P: n/a
another topic though related to the previous post.

How do IF behave in php?

Say,
if( $goahead && copy($somefile1, $somefile2) )
echo "hello world";

What if $goahead is false, will the if the fall, or test all of the
statements?

I can make a complex if system, but eventuallt I can keep it simple
here...
Jan 7 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Mon, 07 Jan 2008 17:24:29 +0100, jodleren <so*****@hot.eewrote:
if( $goahead && copy($somefile1, $somefile2) )
echo "hello world";

What if $goahead is false, will the if the fall, or test all of the
statements?
In PHP, failure of $goahead will mean copy() is not checked/executed in
this case.
I can make a complex if system, but eventuallt I can keep it simple
here...
Well, short from a programmers standpoint indeed. Being more verbose often
doesn't hurt though, and makes code somewhat more flexible/debuggable.
Depends on the actual need offcourse.
--
Rik Wasmus
Jan 7 '08 #2

P: n/a
..oO(jodleren)
>How do IF behave in php?

Say,
if( $goahead && copy($somefile1, $somefile2) )
echo "hello world";

What if $goahead is false, will the if the fall, or test all of the
statements?
PHP uses lazy evaluation. As soon as the result is known, the evaluation
stops. Because of this behaviour you can write things like

$rs = mysql_query(...) or die(...);

Micha
Jan 7 '08 #3

P: n/a

"Michael Fesser" <ne*****@gmx.dewrote in message
news:pm********************************@4ax.com...
.oO(jodleren)
>>How do IF behave in php?

Say,
if( $goahead && copy($somefile1, $somefile2) )
echo "hello world";

What if $goahead is false, will the if the fall, or test all of the
statements?

PHP uses lazy evaluation. As soon as the result is known, the evaluation
stops. Because of this behaviour you can write things like
more pragmatic than lazy. if one condition is false, there is no possible
way other &&'ed conditions could change that result...so, why continue the
evaluation?

most languages do it this way. of those that i've used, vb was the only one
that continued to blindly evaluate conditions. it wasn't until vb.net that
the AndAlso and OrElse short-circuit constructs were added - 'short-circuit'
being even more descriptive than 'lazy' or 'pragmatic'.

cheers.
Jan 7 '08 #4

P: n/a
..oO(Steve)
>"Michael Fesser" <ne*****@gmx.dewrote in message
news:pm********************************@4ax.com.. .
>PHP uses lazy evaluation. As soon as the result is known, the evaluation
stops. Because of this behaviour you can write things like

more pragmatic than lazy. if one condition is false, there is no possible
way other &&'ed conditions could change that result...so, why continue the
evaluation?
Correct, and one common name for that technique is "lazy evaluation" (or
"delayed evaluation"): the result of an expression is only calculated if
and when it's really necessary.
>most languages do it this way. of those that i've used, vb was the only one
that continued to blindly evaluate conditions. it wasn't until vb.net that
the AndAlso and OrElse short-circuit constructs were added - 'short-circuit'
being even more descriptive than 'lazy' or 'pragmatic'.
AFAIK "short-circuit" is more related to logical expressions, whereas
"lazy evaluation" is the more general name and applies to many other
things as well.

Micha
Jan 7 '08 #5

P: n/a

"Michael Fesser" <ne*****@gmx.dewrote in message
news:75********************************@4ax.com...
.oO(Steve)
>>"Michael Fesser" <ne*****@gmx.dewrote in message
news:pm********************************@4ax.com. ..
>>PHP uses lazy evaluation. As soon as the result is known, the evaluation
stops. Because of this behaviour you can write things like

more pragmatic than lazy. if one condition is false, there is no possible
way other &&'ed conditions could change that result...so, why continue the
evaluation?

Correct, and one common name for that technique is "lazy evaluation" (or
"delayed evaluation"): the result of an expression is only calculated if
and when it's really necessary.
>>most languages do it this way. of those that i've used, vb was the only
one
that continued to blindly evaluate conditions. it wasn't until vb.net that
the AndAlso and OrElse short-circuit constructs were added -
'short-circuit'
being even more descriptive than 'lazy' or 'pragmatic'.

AFAIK "short-circuit" is more related to logical expressions, whereas
"lazy evaluation" is the more general name and applies to many other
things as well.
hmmm...i didn't know that. first i'd heard of lazy eval.
Jan 7 '08 #6

P: n/a
On Jan 7, 6:54*pm, "Steve" <no....@example.comwrote:
"Michael Fesser" <neti...@gmx.dewrote in message
PHP uses lazy evaluation. As soon as the result is known, the evaluation
stops. Because of this behaviour you can write things like
most languages do it this way. of those that i've used, vb was the only one
that continued to blindly evaluate conditions. it wasn't until vb.net that
the AndAlso and OrElse short-circuit constructs were added - 'short-circuit'
being even more descriptive than 'lazy' or 'pragmatic'.
Well I would think the same way, but as you wrote, once tried VB one
knows, that one has to be careful...
There are really a lot of languages and scripts out there, so better
safe than not.

As for debugging/reading of code - this can really minimise code, and
by that give a better overview. Some may think it is a bit harder to
read, but I rather have a few lines of code than 3 screens of it.

Thanks, all
Jan 8 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.