How many control statements are there in php?
IntroductionControl 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. Show Types The If statement The ? Operator The switch statement Loops exit, die and return, exceptions Declare The If statement:
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:
? operatorIt 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.
It is mainly used to reduce the size of the code or else if can be used to reduce the complexity. Switch statementSwitch 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.
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. LoopsLoops 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,
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 LoopIt 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.
The statement inside the while loop is executed when the condition is met else it skips the entire loop. Do-while loopThis 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.
Output 10 20 40 For Each statementIt 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.
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):
|