|
PHP Tutorial Home
Basic PHPPHP IntroductionPHP Installation PHP Syntax PHP Variables PHP Form Variables PHP If Statements PHP Switch Statements PHP Arrays PHP While Loops PHP For Loops PHP Operators PHP Functions Advanced PHPPHP IncludePHP Upload File PHP File PHP Mail PHP Database Driven Website PHP Summary Got a MySpace Page?Get "www.yourname.com" for your MySpace page. Learn how >>. |
PHP If StatementsYou use PHP if statements when you want your program to execute a block of code only if a particular condition is true. In other words, you can tell your program "if something is true, then execute this piece of code". Syntax
ExampleIn the following example, we create a variable called "favoriteFruit" and assign a value to it. We then compare the value with a string: "Pomegranate". If the two values are the same, we output some text. The two equals signs (
The above example results in the following:
Your favorite fruit contains around 7% fibre.
If Else StatementWe can add an else to our if statement to make our application do something else if the condition is not true. Example:
The above example results in the following:
Sorry, I don't know how much fibre that fruit contains.
If... Elseif StatementLet's say we learn the fibre content of another fruit. We could then add an elseif to our if statement. That way, we could include a custom message for the new fruit. In fact, we could use elseif as many times as we like. Example:
The above example results in the following:
Your favorite fruit contains around 1.5% fibre.
Enjoy this website?
Oh, and thank you for supporting Quackit! |
Featured Template:
(Build your websites in minutes!) |