Skip to content
Advertisement

PHP – How to add more than 2 parameters to a variable?

So I have the following code:

<ul>
<li>
<p>
<?php
$var = rand(1,2,3,4,5,6,7,8,9,10);
if ($var == 1){
print '<a href="/dir1/file1">link1</a>';
}
if ($var == 2){
print '<a href="/dir2/file2">link2</a>';
}
if ($var == 3){
print '<a href="/dir3/file3">link3</a>';
}
if ($var == 4){
print '<a href="/dir4/file4">link4</a>';
}
if ($var == 5){
print '<a href="/dir5/file5">link5</a>';
}
if ($var == 6){
print '<a href="/dir6/file6">link6</a>';
}
if ($var == 7){
print '<a href="/dir7/file7">link7</a>';
}
if ($var == 8){
print '<a href="/dir8/file8">link8</a>';
}
if ($var == 9){
print '<a href="/dir9/file9">link9</a>';
}
if ($var == 10){
print '<a href="/dir10/file10">link10</a>';
}
?>
</p>
</li>
</ul>

But it outputs this error:

Warning: rand() expects exactly 2 parameters, 10 given…”

So I wonder how could I random the values of this variable and then print() only on in the p tag?

Advertisement

Answer

Since your numbers are in sequence, you could simply do

$var = rand(1,10);   // that will work on range 1 - 10


Function Prototype:

int rand ( int $min , int $max )

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement