2:37 AM

PHP if/else .

posted under by FR3@K | Edit This
In this script, we are going to use the built in PHP function rand and use the if statement and the else statement. These are pretty self explanitory, if something is true, then execute some, else (if it isnt true) then execute something else.



$flip_coin = rand(1,2);

if( $flip_coin == 1 ) {

echo ("You flipped a heads");

} else {

echo ("You flipped a tails");



Ok, in this script we are using the built in PHP function called rand() . It chooses a number at random between the 2 numbers you specified. In this case we specified those numbers to be 1 and 2 ( rand(1,2) ). We assigned this to the variable $flip_coin .

Now we are using an if statement. From here the script is English. Its basically saying if the variable $flip_coin is equal to 1, then echo You flipped a heads. If it doesnt equal 1, it must
equal 2, so echo You flipped a tails.

Notice in these if/else/elseif statements the curly braces { }. They are "consequences" (as such) from the if/else statement.


Make A Comment