php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49061 Notice Undefined Index for "?:" operator
Submitted: 2009-07-26 03:13 UTC Modified: 2009-07-27 02:33 UTC
From: majuki at yahoo dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.10 OS: CenTOS
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: majuki at yahoo dot com
New email:
PHP Version: OS:

 

 [2009-07-26 03:13 UTC] majuki at yahoo dot com
Description:
------------
A version of this bug was reported last year that was marked bogus (Bug #45760: Notice: Undefined index "?:" operator), however, there is an error in this that is not bogus:

(isset($var)) ? doX() : echo $var;

(empty($var)) ? doX() : echo $var;

These return an undefined index even though it's being checked for.

(CentOS)

Also note that the notice in legitimate cases is still undocumented on the alternate control structures page.

Reproduce code:
---------------
(!isset($var)) ? doX() : echo $var;

(empty($var)) ? doX() : echo $var;

Expected result:
----------------
No "Notice: Undefined index"

Actual result:
--------------
"Notice: Undefined index"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-26 12:02 UTC] jani@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.


 [2009-07-26 18:58 UTC] majuki at yahoo dot com
Already included it... but I'll expand on it...


<?php

function doX(){};

(!isset($var)) ? doX() : echo $var;

(empty($var)) ? doX() : echo $var;

?>


Above code with E_NOTICE enabled.)
 [2009-07-26 19:09 UTC] rasmus@php.net
That is a completely bogus test script.  Did you even bother to try running it?  You can't have an echo there and you are claiming an undefined index warning in a script that doesn't even have an index.
 [2009-07-26 19:39 UTC] majuki at yahoo dot com
You're right, my mistake - my original script had it more like the following:

<?php

function doX(){ return 'ok' };

if (!isset($var)) echo $var;

if (empty($var)) echo $var;


echo (!isset($var)) ? doX() : $var;

echo (empty($var)) ? doX() : $var;

?>

Both generated the Notice)
 [2009-07-26 21:29 UTC] rasmus@php.net
Once again your script doesn't run.  There is a syntax error.

Plus, of course you get a warning with:

if (!isset($var)) echo $var;

The warning is on the echo of a variable that hasn't been set, not on the check itself.  Until you provide an actual sensible script that you have run before submitting it, please don't waste any more of our time.
 [2009-07-27 01:55 UTC] majuki at yahoo dot com
wow - harsh. Guess you're having a bad day.  

The code provided, while not my original, does represent the exact syntax that is used and executes as expected.  Yes, there's a syntax error, I put the ; in the wrong place when typing it in.  I'm sorry for not making it so you can cut and paste, it was a typo.

As to my original it is far more complex than this simple extraction and difficult to "sum up" in 10-20 lines.  I will attempt to extrapolate the core logic for you and provide cut and paste code.

File1:

<?php
$var = 1;
include(file2.php);
?>

File2:

<div <?php if (empty($var)) echo 'value="' . $var . '"'; ?>></div>
<div <?php echo (empty($var)) ? '' : 'value="' . $var. '"'; ?>></div>


The above generate the notice where as the following do not:

<div <?php if (empty($var)) { echo $var; } ?>></div>
<div <?php echo (empty($var)) ? 'value="' . $var. '"' : ''; ?>></div>


I hope this is sufficient.  

Please understand that this is my time too - I'm volunteering my time to report what I've observed to be strange and unexpected behaviour, not because it's a problem I need fixed.  All I had to do was turn off E_NOTICE and forget about it, but I thought it might warrant looking into.  So please, next time when someone is reporting something, remember that they didn't have to take the time and if they don't have the time (or in some cases the skill) to provide you with perfect code, be a little understanding.

I hope tomorrow is a better day for you. Sincerely.)
 [2009-07-27 02:33 UTC] rasmus@php.net
There is still no bug here.

Essentially you are saying that:

if(empty($var)) echo $var;

should not generate a warning if $var is undefined.  Whether you write it like that, or like:

echo (empty($var)) ? 'value="' . $var. '"' : '';

is irrelevant.  You have $var in the true part of the ternary there meaning that if $var is empty, which it will be if it is not defined, then try to echo it out.  That will give you that warning, as it should.  You have still not shown us a script that gives an unexpected warning in the ternary operator.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Nov 21 01:00:01 2025 UTC