3

I want to assign random profile pictures to users when they register on a website. I have a problem with my code. If I limit the "if and elseif" statements to 2 conditions, it works well. But when I go over 2 (in this case I have 10 conditions), the code doesn't work. If "switch" statement should be used instead, how would you write the code?

Here is my code

$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";
Nick
  • 138,499
  • 22
  • 57
  • 95
Shea
  • 41
  • 3
  • 4
    Just get rid of the `if` and use one line: `$profile_pic = "/defaults/profile_pic$rand.png";` – Nick Sep 07 '19 at 01:40
  • 1
    "_the code doesn't work_" is not a good description of the problem that you are facing. Stack Overflow questions must have a [mcve] if they are to have a good chance of being retained in this repository of knowledge. Seems like a dupe of https://stackoverflow.com/q/5605965/2943403 – mickmackusa Aug 24 '21 at 22:28

4 Answers4

2

I hope that's what you want

<?php
$rand = rand(1,10);
switch ($rand) {
    case "1":
        $profile_pic = "/defaults/profile_pic1.png";
        echo "1";
        break;
    case "2":
        $profile_pic = "/defaults/profile_pic2.png";
        echo "2";
        break;
    case "3":
        $profile_pic = "/defaults/profile_pic3.png";
        echo "3";
        break;
    case "4":
        $profile_pic = "/defaults/profile_pic4.png";
        echo "4";
        break;
    case "5":
        $profile_pic = "/defaults/profile_pic5.png";
        echo "5";
        break;
    case "6":
        $profile_pic = "/defaults/profile_pic6.png";
        echo "6";
        break;
    case "7":
        $profile_pic = "/defaults/profile_pic7.png";
        echo "7";
        break;
    case "8":
        $profile_pic = "/defaults/profile_pic8.png";
        echo "8";
        break;
    case "9":
        $profile_pic = "/defaults/profile_pic9.png";
        echo "9";
        break;
    case "10":
        $profile_pic = "/defaults/profile_pic10.png";
        echo "10";
        break;
    default:
        $profile_pic = "/defaults/profile_picDEFAULT.png";
        echo "default PHOTO";
}
?>
Wassim Al Ahmad
  • 1,102
  • 2
  • 7
  • 23
1

I really do not understand why you would want to use a switch statement here. In terms of improving the code - you could go with only 2 lines. With if and switch it's 2-3 lines per condition and it's the worst programming practice you might come up with. Just give me a good reason why.

If you read PHP documentation thoroughly, there is the String Operators page available, which explains how your problem might be solved:

concatenation operator (.), which returns the concatenation of its right and left arguments.

$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"

So you could simply do it with

$rand = rand(1,10);
$profile_pic = "/defaults/profile_pic" . $rand . ".png";

Or you might stumble upon Variable parsing, which states:

When a string is specified in double quotes or with heredoc, variables are parsed within it.

$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.";

So what you could do is:

$rand = rand(1,10);
$profile_pic = "/defaults/profile_pic${rand}.png";
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
-1

If you are using the same random number in image path then no need for if or switch loop. You can use below code. $rand = rand(1,10) ;

$profile_pic = "/defaults/profile_pic$rand.png"; If you have mapping of random number and images then store it in array and access it. $rand_image = array(1=>"firstimg.png", 2=>"2.png") ;

$profile_pic = "/defaults/profile_pic".$rand_image[$rand];

Govind
  • 9
  • 2
  • Please take a moment to read through the [editing help](//stackoverflow.com/editing-help) in the [help]. Formatting on Stack Overflow is different than other sites. – Kevin M. Mansour Aug 20 '21 at 21:30
-1
<?php
  if (condition) {
    code to be executed if this condition is true;
  } elseif (condition) {
    code to be executed if first condition is false and this condition is true;
  } else {
    code to be executed if all conditions are false;
  }
?>