Category: 12. PHP

  • Associative Array

    If each element in a PHP array is a key-value pair, such an array is called an associative array. In this type of array, each value is identified by its associated key and not an index. How to Declare an Associative Array in PHP? Both the approaches of declaring an array – the array() function and…

  • Indexed Array

    In PHP, the array elements may be a collection of key-value pairs or it may contain values only. If the array consists of values only, it is said to be an indexed array, as each element is identified by an incrementing index, starting with “0”. An indexed array in PHP may be created either by…

  • Arrays

    An array is a data structure that stores one or more data values having some relation among them, in a single variable. For example, if you want to store the marks of 10 students in a class, then instead of defining 10 different variables, it’s easy to define an array of 10 length. Arrays in…

  • 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…