0
0

More than 1 year has passed since last update.

PHP上から順に実行されることを意識する。

Posted at

rand()関数を使ってスコアに応じてメッセージを変更するプログラムを書こうとしたが、以下のようにrand()関数をwhile分の外(上)に出していたため、while文で10回繰り返す処理に全て同じスコアが入ってしまい意図しない結果となった。

<?php
$try = 1;
$score = rand(0,100);
while ($try <=10){
    echo $try.'回目'.' ';
    echo $score.'点'.' ';
    if($score > 60){
        echo '合格!';
    }else{
        echo '不合格。';
    }
    echo "<br>";
    $try++;
}
?>

while文の処理1回ごとに異なるスコアを出せるようにrand()関数をwhile文の中にセット。

<?
while ($try <=10){
    $score = rand(0,100);
    echo $try.'回目'.' ';
    echo $score.'点'.' ';
    if($score > 60){
        echo 'やったね!合格!';
    }else{
        echo 'ざんねん、不合格。';
    }
    echo "<br>";
    $try++;
}
?>


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0