PHP- Conditional Statements

Conditional statements are basically if and combination of   if and else .There is a special conditional statement named as switch .The conditions statements are take decision for which code have to be executed next with the input.  Basic conditional statements are listed below

  • If
  • If else
  • Nested if
  • Switch

 

If

  •  if  condition returns true or false value.
  • If  if condition return true value then the statements inside the if will be executed
  • If   if condition returns false then the statements inside the if will be omitted.

Syntax

 

Example 1 [ if ]

Output

 

If else

  • In if else the statements executed either if or else part.
  • If   if  condition return true the code inside if will be executed.
  • If   if  condition return false the code inside else will be executed.

Syntax

 

Example 1 [ if else]

Output

 

Nested if

  • In nested if, we can see the conditions in else part also.
  • If first if condition return true the statement inside the if will be executed other will be omitted.If it is return false its check the condition in else part and so on.

Syntax

 

Example 1 [ Nested if ]

output

 

Switch

  • The result from ‘nested if’ and switch conditions are same the difference is switch statement is more simple when compared with ‘nested if’ statement.
  • Switch statement is mainly used when there is a list of choices. Here we check conditions until we get the result.
  • In switch statement there is a default statement is available if all conditions are false then display default statement.

Syntax

 

Example 1 [ Switch ] 

Output

 

Leave a Reply