Reply to Re: Bug || !Bug

Your name:

Reply:


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");

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация