php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18645 dot-operator allows bogus operations with shortcut for if
Submitted: 2002-07-30 09:01 UTC Modified: 2002-07-30 11:45 UTC
From: alpha at rrs dot at Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.2.1 OS: windows 2000
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: alpha at rrs dot at
New email:
PHP Version: OS:

 

 [2002-07-30 09:01 UTC] alpha at rrs dot 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 ...

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-30 11:43 UTC] m dot ford at lmu dot ac dot 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 11:45 UTC] rasmus@php.net
No bug here.  It is consistent with the documented operator precedence.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 05:00:01 2026 UTC