Category: 02. Operators

  • Spaceship Operator

    The Spaceship operator is one of the many new features introduced in PHP with its 7.0 version. It is a three-way comparison operator. The conventional comparison operators (<, >, !=, ==, etc.) return true or false (equivalent to 1 or 0). On the other hand, the spaceship operator has three possible return values: -1,0,or 1.…

  • Null Coalescing Operator

    The Null Coalescing operator is one of the many new features introduced in PHP 7. The word “coalescing” means uniting many things into one. This operator is used to replace the ternary operation in conjunction with the isset() function. Ternary Operator in PHP PHP has a ternary operator represented by the “?” symbol. The ternary operator compares…

  • Spread Operator

    PHP recognizes the three dots symbol (…) as the spread operator. The spread operator is also sometimes called the splat operator. This operator was first introduced in PHP version 7.4. It can be effectively used in many cases such as unpacking arrays. Example 1 In the example below, the elements in $arr1 are inserted in $arr2 after…

  • Conditional Operators Examples

    You would use conditional operators in PHP when there is a need to set a value depending on conditions. It is also known as ternary operator. It first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation. Ternary operators offer…

  • Array Operators

    PHP defines the following set of symbols to be used as operators on array data types − Symbol Example Name Result + $a + $b Union Union of $a and $b. == $a == $b Equality TRUE if $a and $b have the same key/value pairs. === $a === $b Identity TRUE if $a and…

  • String Operators

    There are two operators in PHP for working with string data types: concatenation operator (“.”) and the concatenation assignment operator (“.=”). Read this chapter to learn how these operators work in PHP. Concatenation Operator in PHP The dot operator (“.”) is PHP’s concatenation operator. It joins two string operands (characters of right hand string appended…

  • Assignment Operators Examples

    You can use assignment operators in PHP to assign values to variables. Assignment operators are shorthand notations to perform arithmetic or other operations while assigning a value to a variable. For instance, the “=” operator assigns the value on the right-hand side to the variable on the left-hand side. Additionally, there are compound assignment operators…

  • Logical Operators Examples

    In PHP, logical operators are used to combine conditional statements. These operators allow you to create more complex conditions by combining multiple conditions together. Logical operators are generally used in conditional statements such as if, while, and for loops to control the flow of program execution based on specific conditions. The following table highligts the logical operators that…

  • Comparison Operators Examples

    In PHP, Comparison operators are used to compare two values and determine their relationship. These operators return a Boolean value, either True or False, based on the result of the comparison. The following table highligts the comparison operators that are supported by PHP. Assume variable $a holds 10 and variable $b holds 20, then −…

  • Arithmetic Operators Examples

    In PHP, arithmetic operators are used to perform mathematical operations on numeric values. The following table highligts the arithmetic operators that are supported by PHP. Assume variable “$a” holds 42 and variable “$b” holds 20 − Operator Description Example + Adds two operands $a + $b = 62 – Subtracts the second operand from the…