Re: [PHP4BETA] broken or probably not implement!
| From: | Zeev Suraski | Date: | Fri, 18 Feb 2000 12:41:50 +0000 |
| Subject: | Re: [PHP4BETA] broken or probably not implement! | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-10445@lists.php.net to get a copy of this message | ||
On Fri, 18 Feb 2000, Faisal Nasim wrote:
> : I'm not sure what you're saying here, all of the results reported by PHP
> : are correct. 10||1 is true (boolean). The fact that Perl reports 10 is
>
> Then why:
>
> $str = 10 or 1 // = 10
This is equivalent to:
$str=10;
10 or 1;
Assuming you regard the 'result' as the value of $str, the value of $str
is 10.
> and
> $str = 0 or 1 // = 0
$str=0;
0 or 1;
The value of $str is 0. Generally, $str = n or 1;, would always end up
with $str being equal to n.
>
> 0 is false, and 'or' has low precedence, so result should be 1!
> Also:
> $str = 0 || 1 // = 0
No, ($str = 0 || 1) equals 1:
print ($str=0 or 1);
1
Perhaps you tried
print $str=0 or 1;
in which case, it behaved like this:
$str=0; // expr=0
print 0; // prints '0', expr=1
1 or 1; // expr=1, no side effects (i.e. nothing is printed out).
Remember, or has very low precedence, its even less than print/echo.
I don't have time to dive into the various examples you've given, but I
know that this works quite well, so I'm just trying to explain the
rules...
Zeev
>
>
> $str = "" or "faisal"; // ""
>
> In Perl, it will behave as a conditional operator and won't return
> the boolean match:
>
> $str = "" || "faisal"; // faisal
> $str = "" or "faisal"; // ""
>
>
> BITWISE:
>
> print "" | "faisal"; // ""
> print 10 | 100; // 110
>
>
> All I was trying to simplify this:
>
> $str = (int) $blah;
> if ( ! $str ) $str = 0;
>
> Perl: $str = int $blah || 0;
> PHP: $str = (int) $blah ? $blah : 0;
>
>
> OR:
>
>
> $total = somelongfunction ( $arg );
> if ( ! $total ) $total = 100;
>
> Because this will be silly to call our *long* function twice like this:
> $total = somelongfunction ( $arg ) ? somelongfunction ( $arg ) : 100;
>
> Where, in Perl I can:
> $total = &somelongfunction || 100; # not returning boolean value!
>
>
> : meaningless, any value that translates into true is valid. If you want to
> : conduct bitwise operations, you should be using the bitwise operators,
> : i.e. 10|1.
> :
> :
> : Zeev
> :
> : On Fri, 18 Feb 2000, Faisal Nasim wrote:
> :
> : > Greetings,
> : >
> : > PHP could use some more flexibility and some bug fixing!
> : >
> : > $str = 10 || 1; // 1
> : > $str = 0 || 1; // 1 ... correct
> : > $str = 10 or 1; // 10 ... correct
> : > $str = 0 or 1; // 0
> : >
> : >
> : > Either PHP doesn't support them well now, or its completely
> : > broken! Any info?
> : >
> : > Perl reports:
> : >
> : > 10 ... correct
> : > 1 ... correct
> : > 10 ... correct
> : > 0 ... correct, according to the precedence table, I reckon.
> : >
> : > Thank you!
> :
>
>
--
Zeev Suraski <zeev@zend.com>
http://www.zend.com/