Fellipe Sanches' website

Category: PHP

  • && instead of and, || instead of or?

    You can use AND/OR, but…

    Let’s get straight to the short answer: PHP supports AND/OR instead of &&/||, but because they have lower precedence, they can cause incorrect wait behavior.

    What is Lower precedence?

    Lower precedence is the same as “lower priority”; in this context, we are talking about the order in which an operator is evaluated after other operators in the same expression.

    In other words:
    When PHP reads a line of code with multiple operators, it decides which parts to calculate first based on operator precedence.

    Let’s look at some examples below to clarify the concept.

    Using && / ||

    $a = true;
    $b = false;
    $c = false;
    
    //waited expression
    $res = ($a && $b) || $c;
    //typed expression (it's the same above)
    $res = $a && $b || $c;
    
    
    //practical example
    if($res) {
      echo 'it\'s true';
    }
    else {
      echo 'it\'s false';
    }
    
    //output: it's false

    That makes sense, since express says: if variables a and b are true, or if variable c is true, show “true”, otherwise show “false”.

    Using and / or

    $a = true;
    $b = false;
    $c = false;
    
    //waited expression
    $res = ($a and $b) or $c;
    //typed expression (it isn't the same above)
    $res = $a and $b or $c;
    
    
    //practical example
    if($res) {
      echo 'it\'s true';
    }
    else {
      echo 'it\'s false';
    }
    
    //output: it's true

    Due to and has very low  precedence in PHP, lower than the assignment operator =, so the expression is interpreted as:

    ($res = $a) and $b or $c;

    The rest of the expression, and $b or $c, is then evaluated and discarded because it is not assigned to anything. This logical operation uses the value of $a as its first operand (true).

  • Installing Composer the right way

    If you search online for ways to install Composer on Ubuntu, you’ll probably find suggestions like:

    sudo apt install composer

    It looks simple… but it’s actually one of the worst ways to install Composer.
    Here’s why:

    • It almost always installs an outdated version of Composer.
    • Composer adapts quickly to new PHP versions — Ubuntu packages don’t.
    • apt installs Composer system-wide, which requires sudo even though Composer doesn’t need root privileges.
    • You lose access to composer self-update, because you must wait until Ubuntu updates the package (which often never happens).

    So… let’s install Composer the right way.

    Install Composer in Your Project Root

    In the root of your project run:

    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php -r "if (hash_file('sha384', 'composer-setup.php') === 'c8b085408188070d5f52bcfe4ecfbee5f727afa458b2573b8eaaf77b3419b0bf2768dc67c86944da1544f06fa544fd47') { echo 'Installer verified'.PHP_EOL; } else { echo 'Installer corrupt'.PHP_EOL; unlink('composer-setup.php'); exit(1); }"
    php composer-setup.php
    php -r "unlink('composer-setup.php');"

    This installs Composer directly from the official source.

    At this point, Composer works, but only if you run: php composer.phar.

    Pretty annoying, right?

    Let’s make it easier.

    Create a Local Composer Command (Project Only)

    So, let’s improve this, putting a symlink to your project uses its own composer command.

    In the same folder where composer.phar is located, create a blank file named composer, and edit it:

    touch composer
    vim composer

    Add this content and save it.

    #!/usr/bin/env bash
    php "$(dirname "$0")/composer.phar" "$@"
    

    Back to the shell, make it executable:

    chmod +x composer

    Now your project has its own composer executable…

    s$ ls -l
    total 3416
    -rw-r--r--  1 fellipe fellipe     841 Nov 10 23:01 README.md
    -rwxr-xr-x  1 fellipe fellipe      61 Nov 21 11:38 composer
    -rw-r--r--  1 fellipe fellipe     315 Nov 10 20:27 composer.json
    -rw-r--r--  1 fellipe fellipe  165048 Nov 10 20:27 composer.lock
    -rwxr-xr-x  1 fellipe fellipe 3281618 Nov 20 21:42 composer.phar
    drwxr-xr-x 10 fellipe fellipe    4096 Nov 20 19:46 vendor

    …and you can just run ./composer

    $ ./composer --version
    Composer version 2.9.2 2025-11-19 21:57:25
    PHP version 8.4.15 (/usr/bin/php8.4)

    Summary

    • You don’t put anything in $HOME/bin.
    • You don’t modify your global PATH.
    • You don’t need sudo.
    • You create a small wrapper script that works only inside this project.
    • You gain the flexibility to use different Composer versions per project, and you avoid any security risk from installing system-wide packages as root.

    Nice, right?

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