Читать книгу PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO) - Sergey D Skudaev - Страница 15

If and else statement

Оглавление

The if… else statement is used to execute different pieces of code under different conditions.

It has the following construction:


If (conditions) {

Do this.

} elseif (different condition) {

Do that.

} else {

Do something else.

}


Let us create a function that will print a message depending on temperature.


function weather ($F) {


if ($F> 78)

$message=“It is too hot!”;

elseif ($F <65)

$message=“It is too cold!”

else

$message=“Today is nice outside!”;

Return $message.”<br />”;

}


Let us call the function


echo weather (80);


Output: will be “It is too hot!”;


echo weather (60);


Output: will be “It is too cold!”;


echo weather (70);


Output: will be =“Today is nice outside!”;

PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO)

Подняться наверх