Decision making in php w3schools
Conditional statements are used to perform different actions based on different conditions. Show
PHP Conditional StatementsVery often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this. In PHP we have the following conditional statements:
PHP - The if StatementThe Syntax if (condition) { ExampleOutput "Have a good day!" if the current time (HOUR) is less than 20: $t = date("H"); if ($t < "20") { Try it Yourself » PHP - The if...else StatementThe Syntax
if (condition) { ExampleOutput "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise: $t = date("H"); if ($t < "20") { Try it Yourself » PHP - The if...elseif...else StatementThe Syntax if (condition) { ExampleOutput "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!": $t = date("H"); if ($t < "10") { Try it Yourself » PHP - The switch StatementThe PHP ExercisesPHP conditional statements allow you to make a decision, based upon the result of a condition. These statements are called as Decision Making Statements or Conditional Statements. The flowchart of Decision-making technique in PHP can be expressed as: PHP has such decision-making capabilities within its program by the use of following decision-making statements Conditional Statements in PHP
report this ad View Discussion Improve Article Save Article View Discussion Improve Article Save Article PHP allows us to perform actions based on some type of conditions that may be logical or comparative. Based on the result of these conditions i.e., either TRUE or FALSE, an action would be performed as asked by the user. It’s just like a two- way path. If you want something then go this way or else turn that way. To use this feature, PHP provides us with four conditional statements:
Let us now look at each one of these in details:
Ternary Operators In addition to all this conditional statements, PHP provides a shorthand way of writing if…else, called Ternary Operators. The statement uses a question mark (?) and a colon (:) and takes three
operands: a condition to check, a result for TRUE and a result for FALSE. (condition) ? if TRUE execute this : otherwise execute this; Example:
Output: The number is negative The number is negative This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. What is decision making in PHP?PHP conditional statements allow you to make a decision, based upon the result of a condition. These statements are called as Decision Making Statements or Conditional Statements.
What are the 4 PHP conditional statements?The conditional statements in PHP are if, else, elseif, and switch.
What are control statements in PHP?Control statements are a basic component of all modern-day programming languages like PHP, Java, Python, Go, Ruby, NodeJS. These control statements allow software developers and architects to decide how the software or program they develop will behave under different conditions.
What are the different types of selection statements in PHP?PHP Conditional Statements
if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if...elseif...else statement - executes different codes for more than two conditions.
|