Concept
How do if, if-else, and if-else-if statements work in PHP?
ComputerScienceOne / If, If-Else, If-Else-If Statements
"Conditional statements in PHP utilize the keywords if, else, and else if. Conditions are placed inside parentheses immediately after the if and else if keywords. Examples of all three can be found in Code Sample 38.1. Observe that the statement, if($x < 10) does not have a semicolon at the end. This is because it is a conditional statement that determines the flow of control and not an executable statement. Therefore, no semicolon is used. Suppose we made a mistake and did include a semicolon:\n\n//example of an if statement:\n\nif($x < 10) {\n\nprintf(\"x is less than 10\n\n\");\n\n}\n\n//example of an if-else statement:\n\nif($x < 10) {\n\nprintf(\"x is less than 10\n\n\");\n\n} else {\n\nprintf(\"x is 10 or more\n\n\");\n\n}\n\n//example of an if-else-if statement:\n\nif($x < 10) {\n\nprintf(\"x is less than 10\n\n\");\n\n} else if($x === 10) {\n\nprintf(\"x is equal to ten\n\n\");\n\n} else {\n\nprintf(\"x is greater than 10\n\n\");\n\n}\n\nCode Sample 38.1.: Examples of Conditional Statements in PHP"
Related Ideas
- How are if, if-else, and if-else-if statements used in PHP?ComputerScienceOne · Order of Precedence
- Why does an extra semicolon make the conditional statement ineffective?ComputerScienceOne · Computing a Logarithm
- How are if, if-else, and if-else-if statements written in C?ComputerScienceOne · If, If-Else, If-Else-If Statements
- How do if, else, and else if statements work in Java?ComputerScienceOne · If, If-Else, If-Else-If Statements
- What curly-bracket convention should be used with conditional statements?ComputerScienceOne · Computing a Logarithm
- How are strings, statements, and code blocks written in PHP?ComputerScienceOne · Basic Syntax Rules
- How can PHP conditionals validate income and calculate life and taxes?ComputerScienceOne · Life & Taxes
- How can conditionals prevent a calculated tax liability from becoming negative?ComputerScienceOne · Quadratic Roots Revisited