php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50225 count() on undefined var does not trigger NOTICE
Submitted: 2009-11-19 07:54 UTC Modified: 2009-11-19 18:18 UTC
From: josh dot ribakoff at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.0 OS: Windows Vista
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: josh dot ribakoff at gmail dot com
New email:
PHP Version: OS:

 

 [2009-11-19 07:54 UTC] josh dot ribakoff at gmail dot com
Description:
------------
Example A "compiles" and B does not (E_NOTICE).

Invoking an array index on NULL *probably* warrants my attention more than doing so on an array.

It has cost me countless hours, for one.

Reproduce code:
---------------
// A
$foo = null;
count( $foo['bar'] );

// B
$foo = array();
count( $foo['bar'] );


Expected result:
----------------
2 E_NOTICE errors

Actual result:
--------------
1 E_NOTICE error

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-19 08:32 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

RTFM: 

http://php.net/count 

"There is one exception, if var  is NULL, 0 will be returned."

http://php.net/array

"Converting NULL to an array results in an empty array." 

See also bug #44821
 [2009-11-19 18:18 UTC] josh dot ribakoff at gmail dot com
I disagree with your explanation because $foo['bar'] is never created as $foo still equals NULL according to my debugger.

And to quote the manual:

"Note: Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: an E_NOTICE-level error message will be issued, and the result will be NULL. "

I did RTFM, and it never said anything about trying to access a variable causing it to be created. The explanation left in the related bug you referenced is probably more accurate

"Note: Accessing variables of other types using [] or {} silently returns NULL. "

Even if this is "by design" I still think it should generate a NOTICE, where could I read about the design decisions of why it was done the way it was? And would it be appropriate to re-categorize this as a feature suggestion or do you guys not want to hear about it?
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Sep 20 04:00:01 2025 UTC