Doc #65942 [Opn->Nab]: PHP Notice is fired if the specified $index does not exists

From: Date: Tue, 22 Oct 2013 08:32:23 +0000
Subject: Doc #65942 [Opn->Nab]: PHP Notice is fired if the specified $index does not exists
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-10515@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=65942&edit=1

 ID:                 65942
 Updated by:         krakjoe@php.net
 Reported by:        gontijo dot douglas at gmail dot com
 Summary:            PHP Notice is fired if the specified $index does not
                     exists
-Status:             Open
+Status:             Not a bug
 Type:               Documentation Problem
 Package:            Documentation problem
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

This is expected behaviour; isset/offsetExists does not request a read of the element requested,
only checks for it's presence. 

It would not be useful for isset/offsetExists to raise a warning; it is however expected that if you
read from a non-existent element a warning will be raised, because you have not checked for the
presence of the element but requested the elements data, which doesn't exist.


Previous Comments:
------------------------------------------------------------------------
[2013-10-22 01:11:16] gontijo dot douglas at gmail dot com

Description:
------------
---
From manual page: http://www.php.net/arrayobject.offsetget#refsect1-arrayobject.offsetget-returnvalues
---

It'll fire a PHP Notice if the specified $index does not exists

Test script:
---------------
<?php
// from http://www.php.net/manual/en/arrayobject.offsetget.php#example-3971
$arrayobj = new ArrayObject(array('zero', 7, 'example'=>'e.g.'));
var_dump($arrayobj->offsetGet(1));
var_dump($arrayobj->offsetGet('example'));
var_dump($arrayobj->offsetExists('notfound'));

// added by me
var_dump($arrayobj->offsetGet('notfound')); //shouldn't return null silently?


Expected result:
----------------
int(7)
string(4) "e.g."
bool(false)
NULL


Actual result:
--------------
int(7)
string(4) "e.g."
bool(false)
Notice: Undefined index: notfound in test.php on line 8
NULL


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65942&edit=1


Thread (5 messages)

« previous php.doc.bugs (#10515) next »