How many control statements are there in php?

Introduction

Control statements are conditional statements that execute a block of statements if the condition is correct. The statement inside the conditional block will not execute until the condition is satisfied.

Types

The If statement

The ? Operator

The switch statement

Loops

exit, die and return, exceptions

Declare

The If statement:

  1. if[expression1]  
  2. {  
  3.   Only exceutes when the ic condition is correct.  
  4. }  
  5. elseif[expression2]  
  6. {  
  7.   Executed when the if expression1  
  8.   is false and the expression 2 is true.  
  9. }  
  10. else  
  11. {  
  12.   Executed only when the both if block are false.  
  13. }  

The if statement executes a statement if the expression inside the parenthesis is evaluated to true, or else the code is skipped to the next block. It may be a single statement followed by a semicolon and it is a compound statement surrounded by a curly braces. An else statement may appear immediately after the statement and have a statement of its own. It is executed only when the previous expression is false or else it is not executed.

A simple if statement:

  1.   

? operator

It is represented as a ternary operator and it is used as a conditional operator. It is mainly evaluated to either false or true. If false the expression next to the ternary operator is executed or else expression between the ternary operator and colon is executed.

  1. condition expresion ? true : false;  

It is mainly used to reduce the size of the code or else if can be used to reduce the complexity.

Switch statement

Switch has many expressions and the condition is checked with each expression inside the switch. There is a default statement in the switch which can be used in the else statement and both have the same functionality and execute in the same way. A case is a beginning part for execution.

  1.   

Break is also a control statement used to break the execution of the following statement. In switch it is able to compare two strings and execute the required statement.

When a case statement is executed for the required condition the remaining case statements are skipped only when the break statement is used or else all the case statements are executed in the switch statement.

Loops

Loops are mainly used to repeat the process untill the conditions are met. Until the conditions are  met the loops repeatedly execute. If the the condition is not met, the loop will execute an infinite number of  times.

For loop,

  1.   

Output

value of a is 1

value of a is 2

value of a is 3

value of a is 4

value of a is 5

The loop is executed 5 times beacuse the condition is not satisfied in the next step so it exits the loop and stops repeating the execution.

While Loop

It is the simplest form of looping statement. It checks the expression, and if true it executse the statement or else skips the entire code. It is mainly used when the value is exactly known.

  1.   

The statement inside the while loop is executed when the condition is met else it skips the entire loop.

Do-while loop

This loop is executed once even if the condition is not met. After executing once it will check for conditions and execute the statement until conditions are met.

  1.   

Output

10

20

40

For Each statement

It provides a formalized method for iterating over arrays. An array is a collection of values referenced by keys. The for each statement requires an array and a definition of the variable to receive each element.

  1. foreach [array as key = > value]  
  2. {  
  3.   statement  
  4. }  

What are the control statements in PHP?

Introduction. Control statements are conditional statements that execute a block of statements if the condition is correct. The statement inside the conditional block will not execute until the condition is satisfied.

How many control statement are there?

There are three types of control statements: Conditional/Selection statements. Iteration/Loop statements. Jump statements.

What are the 3 types of control structures in PHP?

PHP supports a number of different control structures:.
elseif..
switch..
while..
do-while..
foreach..

How many types of statements are there in PHP?

PHP has eight principal types of statement [some with slight variants]:

Chủ Đề