具有多个/超过 3 个条件的 php if 和 elseif 语句

我想在用户在网站上注册时为其分配随机的个人资料图片。我的代码有问题。如果我将“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
浏览 348回答 3
3回答

精慕HU

我希望这就是你想要的<?php$rand = rand(1,10);switch ($rand) {&nbsp; &nbsp; case "1":&nbsp; &nbsp; &nbsp; &nbsp; $profile_pic = "/defaults/profile_pic1.png";&nbsp; &nbsp; &nbsp; &nbsp; echo "1";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "2":&nbsp; &nbsp; &nbsp; &nbsp; $profile_pic = "/defaults/profile_pic2.png";&nbsp; &nbsp; &nbsp; &nbsp; echo "2";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "3":&nbsp; &nbsp; &nbsp; &nbsp; $profile_pic = "/defaults/profile_pic3.png";&nbsp; &nbsp; &nbsp; &nbsp; echo "3";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "4":&nbsp; &nbsp; &nbsp; &nbsp; $profile_pic = "/defaults/profile_pic4.png";&nbsp; &nbsp; &nbsp; &nbsp; echo "4";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "5":&nbsp; &nbsp; &nbsp; &nbsp; $profile_pic = "/defaults/profile_pic5.png";&nbsp; &nbsp; &nbsp; &nbsp; echo "5";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "6":&nbsp; &nbsp; &nbsp; &nbsp; $profile_pic = "/defaults/profile_pic6.png";&nbsp; &nbsp; &nbsp; &nbsp; echo "6";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "7":&nbsp; &nbsp; &nbsp; &nbsp; $profile_pic = "/defaults/profile_pic7.png";&nbsp; &nbsp; &nbsp; &nbsp; echo "7";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "8":&nbsp; &nbsp; &nbsp; &nbsp; $profile_pic = "/defaults/profile_pic8.png";&nbsp; &nbsp; &nbsp; &nbsp; echo "8";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "9":&nbsp; &nbsp; &nbsp; &nbsp; $profile_pic = "/defaults/profile_pic9.png";&nbsp; &nbsp; &nbsp; &nbsp; echo "9";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "10":&nbsp; &nbsp; &nbsp; &nbsp; $profile_pic = "/defaults/profile_pic10.png";&nbsp; &nbsp; &nbsp; &nbsp; echo "10";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; $profile_pic = "/defaults/profile_picDEFAULT.png";&nbsp; &nbsp; &nbsp; &nbsp; echo "default PHOTO";}?>

收到一只叮咚

我真的不明白你为什么要在switch这里使用声明。在改进代码方面 - 您只能使用 2 行。随着if和switch它的每个条件2-3行,它是最坏的编程习惯你可能会拿出。只要给我一个很好的理由,为什么。如果您仔细阅读 PHP 文档,则可以使用字符串运算符页面,其中解释了如何解决您的问题:连接运算符 ( .),它返回其左右参数的连接。$a = "Hello ";$b = $a . "World!"; // now $b contains "Hello World!"所以你可以简单地用$rand = rand(1,10);$profile_pic = "/defaults/profile_pic" . $rand . ".png";或者你可能会偶然发现Variable parsing,它指出:当一个字符串用双引号或 heredoc 指定时,变量在其中解析。$juice = "apple";echo "He drank some $juice juice.".PHP_EOL;// Invalid. "s" is a valid character for a variable name, but the variable is $juice.echo "He drank some juice made of $juices.";// Valid. Explicitly specify the end of the variable name by enclosing it in braces:echo "He drank some juice made of ${juice}s.";所以你可以做的是:$rand = rand(1,10);$profile_pic = "/defaults/profile_pic${rand}.png";

慕的地8271018

如果您在图像路径中使用相同的随机数,则不需要 if 或 switch 循环。您可以使用以下代码。$rand = rand(1,10) ;$profile_pic = "/defaults/profile_pic$rand.png";&nbsp;如果您有随机数和图像的映射,则将其存储在数组中并访问它。$rand_image = array(1=>"firstimg.png", 2=>"2.png") ;$profile_pic = "/defaults/profile_pic".$rand_image[$rand];
打开App,查看更多内容
随时随地看视频慕课网APP