if an multiple statements
if an multiple statements
am 07.01.2008 17:24:29 von jodleren
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...
Re: if an multiple statements
am 07.01.2008 17:28:44 von luiheidsgoeroe
On Mon, 07 Jan 2008 17:24:29 +0100, jodleren wrote:
> 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
Re: if an multiple statements
am 07.01.2008 17:35:40 von Michael Fesser
..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
Re: if an multiple statements
am 07.01.2008 17:54:11 von Steve
"Michael Fesser" wrote in message
news:pmk4o3trg1johm5rcaiho04qaos5kbd5rg@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.
Re: if an multiple statements
am 07.01.2008 18:23:24 von Michael Fesser
..oO(Steve)
>"Michael Fesser" wrote in message
>news:pmk4o3trg1johm5rcaiho04qaos5kbd5rg@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
Re: if an multiple statements
am 07.01.2008 18:47:30 von Steve
"Michael Fesser" wrote in message
news:75n4o35njta3ih1ksclpkd5od9moneb149@4ax.com...
> .oO(Steve)
>
>>"Michael Fesser" wrote in message
>>news:pmk4o3trg1johm5rcaiho04qaos5kbd5rg@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.
Re: if an multiple statements
am 08.01.2008 08:42:35 von jodleren
On Jan 7, 6:54=A0pm, "Steve" wrote:
> "Michael Fesser" wrote 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 on=
e
> that continued to blindly evaluate conditions. it wasn't until vb.net that=
> the AndAlso and OrElse short-circuit constructs were added - 'short-circui=
t'
> 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