Category: 03. Control Statements

  • Continue Statement

    Like the break statement, continue is another “loop control statement” in PHP. Unlike the break statement, the continue statement skips the current iteration and continues execution at the condition evaluation and then the beginning of the next iteration. The continue statement can be used inside any type of looping constructs, i.e., for, foreach, while or do-while loops. Like break, the continue keyword is also normally used conditionally. The following flowchart explains how the continue statement works…

  • Break Statement

    The break statement along with the continue statement in PHP are known as “loop control statements”. Any type of loop (for, while or do-while) in PHP is designed to run for a certain number of iterations, as per the test condition used. The break statement inside the looping block takes the program flow outside the block, abandoning the rest of iterations that may be…

  • Do…While Loop

    The “do…while” loop is another looping construct available in PHP. This type of loop is similar to the while loop, except that the test condition is checked at the end of each iteration rather than at the beginning of a new iteration. The while loop verifies the truth condition before entering the loop, whereas in “do…while” loop, the truth…

  • While Loop

    The easiest way to create a loop in a PHP script is with the while construct. The syntax of while loop in PHP is similar to that in C language. The loop body block will be repeatedly executed as long as the Boolean expression in the while statement is true. The following flowchart helps in understanding how the while loop in…

  • Foreach Loop

    The foreach construct in PHP is specially meant for iterating over arrays. If you try to use it on a variable with a different data type, PHP raises an error. The foreach loop in PHP can be used with indexed array as well as associative array. There are two types of usage syntaxes available − The above method is…

  • For Loop

    A program by default follows a sequential execution of statements. If the program flow is directed towards any of earlier statements in the program, it constitutes a loop. The for statement in PHP is a convenient tool to constitute a loop in a PHP script. In this chapter, we will discuss PHP’s for statement. Flowchart of “for”…

  • Loop Types

    Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types. In addition, we will also explain how the continue and break statements are used in PHP to control the execution of loops. PHP for Loop The for statement is used when you know how many times…

  • Switch Statement

    The switch statement in PHP can be treated as an alternative to a series of if…else statements on the same expression. Suppose you need to compare an expression or a variable with many different values and execute a different piece of code depending on which value it equals to. In such a case, you would use multiple if…elseif…else constructs.…

  • If…Else Statement

    The ability to implement conditional logic is the fundamental requirement of any programming language (PHP included). PHP has three keywords (also called as language constructs) – if, elseif and else – are used to take decision based on the different conditions. The if keyword is the basic construct for the conditional execution of code fragments. More often than not, the if keyword is…

  • Decision Making

    A computer program by default follows a simple input-process-output path sequentially. This sequential flow can be altered with the decision control statements offered by all the computer programming languages including PHP. Decision Making in a Computer Program Decision-making is the anticipation of conditions occurring during the execution of a program and specified actions taken according…