note 93517 deleted from function.array-rand by danbrown
| From: | danbrown@php.net | Date: | Mon, 14 Sep 2009 16:58:23 +0000 |
| Subject: | note 93517 deleted from function.array-rand by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160555@lists.php.net to get a copy of this message | ||
Note Submitter: dyer85 at gmail dot com
----
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];
?>