note 93517 added to function.array-rand
| From: | dyer85 at gmail dot com | Date: | Mon, 14 Sep 2009 10:08:23 +0000 |
| Subject: | note 93517 added to function.array-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160537@lists.php.net to get a copy of this message | ||
webmaster at askapache dot com's note on 2 May 2009 has a couple potential off-by-one errors by
using sizeof()'s return value as the second argument for the rand() calls. You would need to
subtract one from sizeof()'s return value:
<?php
$array = array('one', 'two', 'three');
$idx = rand(0, sizeof($array) - 1);
echo $array[$idx];
?>
----
Server IP: 209.41.74.194
Probable Submitter: 72.67.36.93
----
Manual Page -- http://www.php.net/manual/en/function.array-rand.php
Edit -- https://master.php.net/note/edit/93517
Del: integrated -- https://master.php.net/note/delete/93517/integrated
Del: useless -- https://master.php.net/note/delete/93517/useless
Del: bad code -- https://master.php.net/note/delete/93517/bad+code
Del: spam -- https://master.php.net/note/delete/93517/spam
Del: non-english -- https://master.php.net/note/delete/93517/non-english
Del: in docs -- https://master.php.net/note/delete/93517/in+docs
Del: other reasons-- https://master.php.net/note/delete/93517
Reject -- https://master.php.net/note/reject/93517
Search -- https://master.php.net/manage/user-notes.php