Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Return type in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 158
    Comment on it

    Welcome to Findnerd. Today we are going to discuss the return types in php which is improved in PHP7. In php 7 you can set the data type for the return value of the function. You need to set the declare(strict_types=1) in the file top.

    <?php
    declare(strict_types=1);
    function userCal(int num1, int num2) : int{
    if(num2==0){
    return 999;
    }else{
    return ($num1/$num2);
    }
    }
    $data = userCal(90,2);
    $data = userCal(4,3);
    ?>
    

    In above example we have set the strict typing mode. We created a function which should be return the integer type value. We called this function twice. In second call, we passed two integer which we produce the float value so it will return the error of return type

 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: