'Replacing Numbers with text in php [closed]

So my taslk is to program two numbers randomly (for example 0 and 1) and then replace the numbers with "Hello" and "bye". I've done the generating it rendomly part, but now I'm struggling with the replacing Part. It would be cool If the solution would be by using "if" and "else" Here's what i've done so far,

thank you in advance

<?php

    $zaehler = 0;

while($zaehler < 10)
    {

    
    echo mt_rand(0, 1);
    
    if(0)
    {
        echo "bye" ;
    }
    
    else (1) ;
    {
        echo "hello" ;
    }
    $zaehler++;
    }
?>


Solution 1:[1]

For your case, just use a comparison statement

e.g.

if($result==0) { // do something;} else {// do another thing; }

(the $result will only be either 0 or 1, so just use one if-then-else)

<?php

    $zaehler = 0;

 while($zaehler < 10){

    $result=mt_rand(0, 1);
    echo $result;

      if($result==0) {
         echo "bye" ;
         } else {
          echo "hello" ;
      }

    $zaehler++;
 }

?>

Solution 2:[2]

So i just saw your post,

First, you need to assign mt_rand to a variable. Afer your statement IF isnt correct, you need to evaluate something like this $i === 0 And about your else you need an else if, so you can take a look to this code ;)

$idx = 0;

while ($idx < 10) {
    $random_int = mt_rand(0, 1);
    if ($random_int === 0) {
        echo "bye\r\n";
    } else if ($random_int === 1) {
        echo "hello\r\n";
    }
    $idx++;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Amity