php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45320 Ternary-Operator Shortcut and empty()
Submitted: 2008-06-20 08:12 UTC Modified: 2008-06-20 10:13 UTC
From: reto at buxaprojects dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 6CVS-2008-06-20 (snap) OS:
Private report: No CVE-ID: None
 [2008-06-20 08:12 UTC] reto at buxaprojects dot com
Description:
------------
The Ternary-Operator shortcut doesn't do the same with empty()

Reproduce code:
---------------
ini_set('display_errors', true);
error_reporting(E_ALL|E_STRICT);

$var = 'Something';

echo !empty($var) ? $var : 'Nothing';
echo !empty($var) ?: 'Nothing';

Expected result:
----------------
SomethingSomething

Actual result:
--------------
Something1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-20 10:13 UTC] colder@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.

$a ?: $b; is equivalent to $a ? $a : $b;

so in your code:
!empty($var) ?: 'Nothing' 
is equivalent to :
!empty($var) ? !empty($var) : 'Nothing';
and not
!empty($var) ? $var : 'Nothing';
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 01 10:01:30 2024 UTC