PHP

Scalar Type Declaration

Scalar Type Declaration

Hello Everyone, Welcome to my blog. First of all, for those people who don’t know anything about me please check out this link for more information. 

If you want to connect with me or want to discuss a specific topic, want to share your view or anything, please feel free to write a comment below or write me an email on my email id i.e ranade.ashish11@gmail.com. I will definitely try to address all your queries. Let’s get started.

Today, I like to share some of the key features provided in PHP. The PHP community has introduced some good features like Scalar type declaration, Return type declarations, etc.., which add some extra features to the PHP development, and provides flexibility in the development lifecycle. In this blog, I would like to explain the Scalar type declaration feature, I hope this will help you to improve your knowledge and help you somehow in the future.

Scalar Type Declaration

Type declaration is also known as Type Hinting, allows a function to accept a parameter of a certain type if the given value is not of the correct type (Integer, Float, Boolean, string) PHP will return a fatal error. The older version of PHP (PHP 5) gives a recoverable error while the new release (PHP 7) returns a throwable error.

PHP 7 has introduced the following Scalar type declarations.

  1. Boolean
  2. String
  3. Float
  4. Integer
  5. Iterable

Scalar Type Declaration comes in two types Coercive(i.e. default), and another one is Strict, if we want to enforce PHP to use a strict type declaration then, we should set the strict value to 1 using a declare directive, please check the example below.

[php]declare(strict_types=1); [/php]

Please check the example below for Coercive and Strict type declarations.

Coercive Scalar Type Declaration

[php]try{function sum(int …$ints){return array_sum($ints);}}catch(Exception $ex){echo $ex->getMessage();}print(sum(2, ‘3’, 4.1));[/php]

In the above example just want to highlight one thing i.e. we are not using a strict value for parameter type like 2 of Integer type, 3 of String type 4.1 of Float type. The above code will out put the value int(9).

Strict Scalar Type Declaration

[php] declare(strict_types=1);try {function sum(int …$ints){return array_sum($ints);}} catch (Exception $ex) {echo $ex->getMessage();}print(sum(2, ‘3’, 4.1));[/php]

Note that, if we declare the strict_type value to 1, the above code will output “Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given”.

The above code will help us to enforce the other developer to use a variable of strict type i.e. Integer, Float, etc.

Use of Scalar Type Declaration

The scalar type declaration can be used in different areas of development like defining strict values in an interface so that the child class which is overriding the interface must follow the strict type.

Please find a link for your reference and a more detailed explanation.

I hope that will help you somehow. Feel free to leave a comment below. Let me know what you think about this blog. Goodbye for now and happy coding 🙂

[post-views]

Tagged

1 thought on “Scalar Type Declaration

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.