Menu Close
    Start a Project Request quote Reading list Switch to dark mode

    PHP Scalar and Return type Declarations

    Hey, if you are looking into PHP scalar or return type declarations then you must read this post. These are the features that were introduced in PHP 7

    Now let’s look closely with examples.

    Scalar Type Declarations

    This type of declaration has two options coercive (default mode) and strict

    Coercive mode: it’s a default mode

    In the above example, it’s just a simple customer class and method setWalletBalance() uses scalar type declaration which will accept any number of values then make a sum and then set to property balance.

    If you just look into variables $registration_reward and $newsletter_reward have different data types so in coercive / default mode the program will output “My total balance is 1100” without any error.

    strict mode

    In the above example, I just enabled strict mode on the very first line of my program so now you will get a fatal

    Return Type Declarations

    It specifies the type of value which will be returned from a function

    Default behavior:

    In the above example, I am just adding multiple values for an address using the scalar declaration as we looked into the previous example.

    For method hasAddress(), it will return boolean value because the return type is bool but without any error then program output:

    Strict mode:

    If you enable the strict mode as I did in the above example at the very first line of my program then it will give a fatal because hasAddress() method is set to return only boolean value and address property has the string value

    That’s it for this post, I hope it was helpful for you. visit more blogs here and check other PHP 7 features on the official site here.

    If you have any query then ask in the comment section. Thanks

    . . .


    Add Your Comment

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again