#18645 [Opn->Bgs]: dot-operator allows bogus operations with shortcut for if
| From: | rasmus@php.net | Date: | Tue, 30 Jul 2002 15:45:17 +0000 |
| Subject: | #18645 [Opn->Bgs]: dot-operator allows bogus operations with shortcut for if | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-15596@lists.php.net to get a copy of this message | ||
ID: 18645
Updated by: rasmus@php.net
Reported By: alpha@rrs.at
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: windows 2000
PHP Version: 4.2.1
New Comment:
No bug here. It is consistent with the documented operator precedence.
Previous Comments:
------------------------------------------------------------------------
[2002-07-30 11:43:26] m.ford@lmu.ac.uk
Let's see, from http://www.php.net/manual/en/language.operators.php,
.
has higher precedence than ?:, so this:
> echo "string1 ".empty($var)?0:$var." string2";
resolves to:
echo ("string1 ".empty($var))?0:($var." string2");
which seems like it'll echo "0" to me...
The answer, as you rightly show, is to use parentheses to override the
precedence.
On your other error messages:
> echo 'string1 '.empty($var)?0.' string2';
is just plain wrong since the operator is ?: and must have all parts
present; but I guess it also fails because the parser is reading "0."
as a floating number and objecting to the immediately following string
quote. This would also apply to:
> echo 'string1 '.0.' string2';
Again, the solution would be to wite it in a way that is not ambiguous,
e.g.:
echo 'string1 ' . 0 . ' string2';
Cheers!
Mike
------------------------------------------------------------------------
[2002-07-30 09:01:44] alpha@rrs.at
given this code
<?php
$var = 5;
echo "string1 ".empty($var)?0:$var." string2";
// output "0" - WRONG
echo "<br>";
echo "string1 ".(empty($var)?0:$var)." string2<br>";
// output "string1 5 string2"
echo "<br>";
echo 'string1 '.empty($var)?0:$var.' string2';
// output "0" - WRONG
echo '<br>';
echo 'string1 '.(empty($var)?0:$var).' string2<br>';
// output "string1 5 string2"
echo '<br>';
?>
I get an error-message if I try
echo 'string1 '.empty($var)?0.' string2';
or just
echo 'string1 '.0.' string2';
, thus there is a missing error check at parse time. I don't know
enough about the internal functions of PHP to say if it's the scripting
engine or something else. See yourself ...
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=18645&edit=1