Fellipe Sanches' website

Author: F. Sanches

  • What’s new in php 8

    Topic index on this page

  • Important changes in PHP 8

    Topic index on this page

    The way that automatic numbering works

    If you create an array with the first key as a negative number, subsequent keys will add 1 to the previous number. Prior to PHP 8, subsequent keys after a negative number always started from zero.

    <?php
    
    //php >= 8.x
      $myArray[-3] = 'R';
      array_push($myArray, 'T', 'Y');
      print_r($myArray);
    /*OUTPUT 
    Array
    (
        [-3] => R
        [-2] => T
        [-1] => Y
    )*/
    
    //php < 8.x
      $myArray[-3] = 'R';
      array_push($myArray, 'T', 'Y');
      print_r($myArray);
    /*OUTPUT 
    Array
    (
        [-3] => R
        [0] => T
        [1] => Y
    )
    )*/
    
    

    Comparison between numbers and strings

    PHP 8 changes the way the loose comparison operator == compares numbers to strings. Now it converting the number to a string and testing whether they’re the same.

    Before, the comparison was performed converting the string to a number, and it may seem bizarre, but previously any string compared no started in a numeral was converted to zero, what using the == operator would return true. Now all this sentenses (0 == “not-a-number”) are false:

    0 == "foo" is false

    0 == "" is false

    0 == "0" is true

    0 === "0" is false

    The words ´match´ and ´mixed´ are now reserved keywords

  • What’s new in php 7

    Topic index on this page

    Spaceship operator <=>

    The spaceship operator <=> is used for comparing two expressions.

    It returns:

    -1 when the left side is smaller

    0 when the left side is equal

    1 when the left side is greater

    Underscores in Integer & Floating notation

    Like always, whole number, such as 3, 14, 22, or 2025 should not contain commas. However, since PHP 7.4, you can use underscores between digits for better readability, for example, 2_025. The PHP engine will remove the underscores automatically.

    Floating point numbers (also known as floats, doubles, or real numbers) can be specified using decimal point, such as Pi 3.14159265359 or 2500.75, likewise, since PHP 7.4, you can use underscores between digits for better readability, for example, 2_500.75.