Posted by Hilarion on 08/22/05 16:07
> <?php
>
> function foo($bar)
> {
> return print($bar);
> }
>
> if ( print("a") | print("b") );
> echo "<br>";
> if ( print("a") || print("b") );
> echo "<br>";
> if ( print("a") or print("b") );
> echo "<br>";
>
> echo "<br>";
> if ( foo("a") | foo("b") );
> echo "<br>";
> if ( foo("a") || foo("b") );
> echo "<br>";
> if ( foo("a") or foo("b") );
>
> ?>
>
> What is the difference?
"print" is not a function (it is a language construct) and not
understanding this causes misinterpretation of parentheses
(round brackets) - they do NOT isolate parameters in case
of "print".
So:
print("a") | print("b")
is equal to:
print "a" | print("b")
which is equal to:
print( "a" | print("b") )
which causes printing of "b" and result of ("a" | 1) which is 1.
print("a") || print("b")
is equal to:
print( ("a") || print("b") )
Expression (("a") || anything) is always true and because of
that the above expression is evaluated to print(true), which
prints 1.
print("a") or print("b")
is equal to:
(print("a")) or (print("b"))
First "print" prints "a" and returns 1. In this case rest
of expression is not evaluated and that's why "b" is not printed.
"foo" is a function, so parentheses do isolate parameters.
Hilarion
PS.: You do not have to use "if" to evaluate expressions.
if ( print("a") | print("b") );
is equal to
print("a") | print("b");
Navigation:
[Reply to this message]
|