Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to define Strictly Type Casting in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 654
    Comment on it

    Variable declaration are the weak part of type casting in PHP, therefore we call PHP as a weak and dynamic.

    PHP7 has introduced an addition optional feature for each file, we can set all function calls and return statements with in a file, can have “strict” type-checking for scalar type declarations. A new declare() directive is added, strict_types, which should be either 0,1.

    In simple words, we can say that  'type casting' is an expression used for generating a new value of a different type of the original. There is no need for the variable in case there is one involved.

     

     

    Lets see and understand this feature with example:

    For Strict Type :: 0

    Here PHP does not cast any variable so hard for specific variable definition and declaration. Lets see in the below example if you can make mistake like float variable define as int but this will not prompt any error.

    Which makes us clear that Strict type 0 set no hard rule for variable declaration and definition.

    <?php 
    declare(strict_types=0); // it should be define on top of each file otherwise you will get error
    
    function findSum(float $numberOne, float $numberTwo) {
        return $numberOne + $numberTwo;
    }
    
    Example One:
    findSum(2.5, "1 week"); // findSum will treat 2.5 as 2.5 and 1 week(string) will be cast as 1.0
    
    Example Two
    findSum(2, "1"); // findSum will treat 2(int) as 2.0 and 1(string) will be cast as 1.0
    
    
    Example Three
    findSum(2.8, 1); // findSum will treat 2.8(float) as 2.8 and 1(int) will be cast as 1.0
    
    ?>
    

     

    For Strict Type :: 1

    Here PHP cast any variable so hard for specific variable definition and declaration. Like in the below example you can see if we assign any float variable as string value it will prompt error and force you assign any float value to this.

    It clearly states that Strict type 1 set hard rule for variable declaration and definition.

    <?php 
    declare(strict_types=1); // it should be define on top of each file otherwise you will get error
    
    function findSum(float $numberOne, float $numberTwo) {
        return $numberOne + $numberTwo;
    }
    
    Example One:
    findSum(2.5, "1 week"); // Fatal error: Uncaught TypeError: Argument 2 passed to findSum() must be of the type float, string given
    
    Example Two
    findSum(2, "1"); // Fatal error: Uncaught TypeError: Argument 2 passed to findSum() must be of the type float, string given
    
    
    Example Three
    findSum(2.8, 1); // findSum will treat 2.8(float) as 2.8 and 1(int) will be cast as 1.0(float)
    
    ?>

     

    Thanks for the read. Please feel free to share your views regarding Strictly Type Casting in PHP.

    How to define Strictly Type Casting in PHP

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: