code like a pro in php 2022 | Best Practices and code improvements

    By: Manu
    2 years ago
    Category: PHPViews: 37

    Hi to day we are going to see how we can improve the way write our code. Here are few examples.

    Let's start with if conditions

    so in general we write a if statement like this

    if(1 === 1){
          echo "Number is equal";
    }else{
         echo "Number is Not equal";
    }
    

    Nothing much but we have a condition and the echoing out data accordingly.

    But we can improve it using ternary Operator in PHP and also in javascript. Here is how you do in PHP.

    echo 1===1 ? "Number is equal" : "Number is Not equal";
    

    Same code execution but in single line

    Here "?" divides the code anything before is condition and code after "?" is condition is true. code after ":" executes if condition is false.


    Use of continue and break

    If we are dealing with loop then we can use continue and break here is an example

    foreach($arr as $array){
        if($array === 'roger'){
              continue;
        }
        
         echo $array."<br>";
    }
    


    Here if array value equals 'roger', We are continuing the iteration and going to next iteration without executing code after if statement.

    Similarly "Break works" but in case of break we break out of loop.

    Let's See what is Null Coalescing Operator


    $value = null;
    echo $value ?? 13;
    

    Ok so we have $value as "null" below statement with output 13 as it will check the $vale but as it is null it will output content after "??".

    $value = 12;
    echo $value ?? 13;
    

    Here output will be 12 as $value variable is not null and has value in it.


    What is Spaceship Operator

    PHP 7 brings this operator and it behaves this way

    Return 0 if values on either side are equal
    Return 1 if value on the left is greater
    Return -1 if the value on the right is greater
    
    echo 1 <=> 1 // outputs 0
    echo 1 <=> 2 // outputs -1
    echo 2 <=> 1 // outputs 1
    

    Check our video guide for more clarity.


    How to assign values to multiple variables in php


    ok here is an example

    $number1 = 45;
    $number2 = 45;
    $number3 = 45;
    

    Here we set values to 3 variables. But we can do it this way as well in one line.

    $number1 = $number2 = $number3 = 45;
    

    Is't it better more readable. Yeah Senior developers use code like this.

    Now you assigned values to multiple variables, You might want to print multiple values at once for debugging of your code. Here is how you do it.

    var_dump($number1, $number2, $number3);
    

    It will print values for all variables.

    At last but not least use "===" more if possible, Instead "==".

    There is a risk if data type is different it might behave unexpectedly but if you aware what data should come "===" is better choice.

    $name1  = '12';
    $name2  = 12;
    
    
    echo $name1 === $name2; 
    
    // Not equal returns 0 as $name2 is integer but $name1 is string
    
    echo $name1 == $name2; 
    
    // Equals as we are not checking type but just checking data in both variables.
    

    Hope this helps.