php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #31399 PHP5 incorrect count description
Submitted: 2005-01-04 00:27 UTC Modified: 2005-01-05 09:02 UTC
From: sveta at microbecal dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5CVS-2005-01-04 (dev) OS:
Private report: No CVE-ID: None
 [2005-01-04 00:27 UTC] sveta at microbecal dot com
Description:
------------
In function.count I see:
"count() may return 0 for a variable that isn't set", but is not true with PHP5 with E_NOTICE turned on

I have tried to patch it:

--- count.xml	2004-11-03 10:30:02.000000000 +0300
+++ count_new.xml	2005-01-01 19:45:24.000000000 +0300
@@ -53,6 +53,8 @@
       isn't set, but it may also return 0 for a variable that has
       been initialized with an empty array. Use
       <function>isset</function> to test if a variable is set.
+      Since PHP 5.0.0, <function>count</function> generates warning of level
+      <link linkend="errorfunc.constants">E_NOTICE</link> in case of taking non-initialized variable as parameter.
      </para>
     </caution>
     <para>

Reproduce code:
---------------
count($x);

Expected result:
----------------
0

Actual result:
--------------
Notice: Undefined variable: x in D:\My Documents\web_project\PHP.lessons\www\reflection.php on line 3

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-04 12:15 UTC] nlopess@php.net
It stills return 0.
Try echoing its value with: '<? echo count($a); ?>'
 [2005-01-04 12:46 UTC] derick@php.net
That was not the point Nuno, thebehavior changed regarding non-initialized values..that can be documented just fine.
 [2005-01-04 17:00 UTC] nlopess@php.net
PHP 4 also gives a warning:

C:\php4>php-cli -r "error_reporting(E_ALL); echo count($a);"

Notice: Undefined variable:  a in Command line code on line 1
0
 [2005-01-04 18:49 UTC] sveta at microbecal dot com
OK Lets correct "patch" to:

--- count.xml	2004-11-03 10:30:02.000000000 +0300
+++ count_new.xml	2005-01-01 19:45:24.000000000 +0300
@@ -53,6 +53,8 @@
       isn't set, but it may also return 0 for a variable that has
       been initialized with an empty array. Use
       <function>isset</function> to test if a variable is set.
+      <function>count</function> generates warning of level
+      <link linkend="errorfunc.constants">E_NOTICE</link> in case of taking non-initialized variable as parameter.
      </para>
     </caution>
     <para>

or to something with similar sence, but better style English
 [2005-01-04 18:49 UTC] sveta at microbecal dot com
OK Lets correct "patch" to:

--- count.xml	2004-11-03 10:30:02.000000000 +0300
+++ count_new.xml	2005-01-01 19:45:24.000000000 +0300
@@ -53,6 +53,8 @@
       isn't set, but it may also return 0 for a variable that has
       been initialized with an empty array. Use
       <function>isset</function> to test if a variable is set.
+      <function>count</function> generates warning of level
+      <link linkend="errorfunc.constants">E_NOTICE</link> in case of taking non-initialized variable as parameter.
      </para>
     </caution>
     <para>

or to something with similar sence, but better style English
 [2005-01-04 21:36 UTC] nlopess@php.net
I don't agree with that change, because if we add such info to count() we would have to add it to all functions.

If you use an uninitialized var in PHP, you will get a E_NOTICE. This isn't specific to count().

Nuno
 [2005-01-04 21:51 UTC] sean@php.net
sean@iconoclast:~$ echo '<?php error_reporting(E_ALL); echo PHP_VERSION ."\n". count($a) ."\n"; ?>' | php

Notice: Undefined variable:  a in - on line 1
4.3.4
0
sean@iconoclast:~$ echo '<?php error_reporting(E_ALL); echo PHP_VERSION ."\n". count($a) ."\n"; ?>' | php5

Notice: Undefined variable:  a in - on line 1
5.0.3
0


I agree with Nuno: E_NOTICE on undefined variable access is (almost) global, so we should not start documenting this everywhere.

5.0.3 DOES return 0 if undefined.

Derick: If something changed, what is it?

S

 [2005-01-05 09:02 UTC] derick@php.net
Well... nothing then, as I thought he claimed that on 4.3 it wouldn't throw a notice.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jul 07 10:01:31 2024 UTC