我想在用户在网站上注册时为其分配随机的个人资料图片。我的代码有问题。如果我将“if 和 elseif”语句限制为 2 个条件,则效果很好。但是当我超过 2(在这种情况下我有 10 个条件)时,代码不起作用。如果应该使用“switch”语句来代替,您将如何编写代码?
这是我的代码
$rand = rand(1,10); //random number between 1 and 10
if($rand == 1)
$profile_pic = "/defaults/profile_pic1.png";
else if($rand == 2)
$profile_pic = "/defaults/profile_pic2.png";
.
.
.
else if($rand == 9)
$profile_pic = "/defaults/profile_pic9.png";
else
$profile_pic = "/defaults/profile_pic10.png";
哔哔one
精慕HU
收到一只叮咚
慕的地8271018
随时随地看视频慕课网APP