php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79160 String Concatenation along with condition/ternary operator yields wrong result.
Submitted: 2020-01-23 13:25 UTC Modified: 2020-01-23 13:40 UTC
From: divyaedward89 at gmail dot com Assigned: sjon (profile)
Status: Not a bug Package: Strings related
PHP Version: 7.2.27 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: divyaedward89 at gmail dot com
New email:
PHP Version: OS:

 

 [2020-01-23 13:25 UTC] divyaedward89 at gmail dot com
Description:
------------
---
From manual page: https://php.net/language.operators.string
---

String Concatenation along with condition/ternary operator yields wrong result.
The condition does not execute as expected. The condition affects the string concatenation.

Test script:
---------------
<?php

$var='I am not empty';
$query = '';
$query .= 'Value of var='.(empty($var)) ? '0' : $var;
echo $query;

?>

Expected result:
----------------
Value of var=I am not empty


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-23 13:32 UTC] sjon@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: sjon
 [2020-01-23 13:32 UTC] sjon@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

https://www.php.net/manual/en/language.operators.precedence.php

the proper way to write this is:

> $query .= 'Value of var='.(empty($var) ? '0' : $var);
 [2020-01-23 13:40 UTC] nikic@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC