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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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

Add a Patch

Pull Requests

Add a Pull Request

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: Fri Mar 29 04:01:29 2024 UTC