note 36413 added to language.references.return

From: Date: Wed, 08 Oct 2003 22:54:38 +0000
Subject: note 36413 added to language.references.return
Groups: php.notes 
Request: Send a blank email to php-notes+get-58078@lists.php.net to get a copy of this message
In the following example, if $key does not exist in the "private" $arr when calling get_value_for($key), then $this->arr[$key] will be set, which is not the intended action. class silly_array { var $arr; ... function & get_value_for($key) { return $this->arr[$key]; } ... } Therefore, one should check if $key exists in the array first, before returning its value. Something like this: function & get_value_for($key) { return isset($this->arr[$key]) ? $this->arr[$key] : false; } ---- Manual Page -- http://www.php.net/manual/en/language.references.return.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+36413 Delete -- http://master.php.net/manage/user-notes.php?action=delete+36413&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+36413&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#58078) next »