Web Programming TutorialsLearn How to Write Scalar And Return Type Declarations in PHP7

Learn How to Write Scalar And Return Type Declarations in PHP7

In the last chapter, we completed the installation of four key components and the integration of PHP 7 with eclipse MARS version. Let’s begin this chapter with a small demo to make sure our environment setup is working perfectly.

Following are the step by step instructions to create a new PHP project in eclipse and run that project on Apache web server with PHP 7 parser.
Step 1: On the left hand side of eclipse, you will find PHP explorer. Right click on the white space and select New -> Project… It will open up a new project wizard dialogue box as shown below. From the wizard choose ‘PHP Project’ and click on the ‘Next’ button.
Step 2: This will open up another dialogue box which asks you to create a PHP Project by entering a Project name and the local server directory to deploy this project. Since we have installed XAMPP application where apache server has its DocumentRoot at “C: /xampp/htdocs” as shown below. Therefore, create a similar directory name as ‘php7-demo-examples’ at this document root location. Apache server will parse the PHP web pages from here that can be accessed from the default URL as http://localhost:80/php7-demo-examples/<newPHPFile>.php. After this set up click on the Finish button to complete the PHP project creation.
Step 3: Once PHP project is created, it will be visible on the left hand side on the PHP explorer pane. Right click on the project root directory and navigate through New -> PHP File to add a PHP file to the current project as shown below.
Step 4: This will open up a new dialogue box which will ask to enter the PHP file name as shown below. Give a PHP file name (like DemoProject.php). Click on the Next button to complete this step.
Step 5: It will open up a PHP template page to select a PHP template. Choose the default PHP template as ‘New simple PHP file’ as shown below then click on the Finish button.
Step 6: Edit the DemoProject.php file in eclipse and write the basic PHP code as shown below.
Step 7: At this step we are all set, just make sure that XAMPP apache server is running on the port 80 before we can run this PHP program. To run this program, right click on the PHP file in the PHP explorer and navigate as Run As -> 2 PHP Web Application as shown below.
Step 8: It will open up a dialogue box as shown below. As discussed before the web page URL is http://localhost/php7-demo-examples/DemoProject.php . Click on the OK button to see the output.
Step 9: Given below is the output for DemoProject.php.
This concludes that our environment setup for PHP7 is perfect and now we can explore new features of PHP 7 starting with the current topic as ‘PHP 7 Scalar and Return Type Declarations’.

Scalar Type Declarations: PHP 7 Scalar type declaration has the following two mode options:

  • Coercive Mode: it is the default mode so we need not to declare it as a scalar type mode in the PHP script.
  • Strict Mode: We must explicitly declare this mode as a scalar type. g. declare (strict_types=1). This is to be noted that strict_types declaration must be the first statement in the PHP script.

Using above two modes, we can enforce the following function parameters.

  1. Int
  2. Float
  3. Bool
  4. String
  5. Interfaces
  6. Array
  7. Callable

Return Type Declarations: Like the scalar type declarations, return type declarations is also one of the new features that is added to PHP 7. In return type declaration, we specify the return type of the value that a PHP function should return. Following are the return types in PHP 7 that could be declared.

  1. Int
  2. Float
  3. Bool
  4. String
  5. Interfaces
  6. Array
  7. Callable

We will understand these declarations with the following PHP examples.

Type 1: Coercive Mode Scalar Type Declaration.
In the below program, we are not declaring the scalar type. Therefore, by default it is in coercive mode. The function ‘add’ accepts an array of integers which is returning the array sum of the integer values. When we are calling this ‘add’ function inside the print command, there we are entering first argument as an integer (6), second argument as a string (‘4’), third argument as a floating number (8.7) and fourth argument as a Boolean value (true). Since this is a coercive mode scalar type declaration, therefore, PHP parser has added up the integer equivalent part of these values all together irrespective of their data types.
Output: In the coercive mode, 6 + 4 + 8 +1 = 19. Boolean true value is 1 and false value is 0 as their integer part. Therefore, addition of these values equals to 19 that is displayed in the output.

Type 2: Strict Mode Scalar Type Declaration.
In order to make the above program to run in the strict mode scalar type declaration, we have added strict_types declaration as the first statement in the script. Since PHP 7 parser is strictly enforcing the data types as integer values for the add function therefore a fatal error has occurred after running this PHP program. If we give an array all integer values as input then the same program will execute without any error.
Output: In the strict mode, the add function expects an array of integer values only. Here, since we have passed string, float and Boolean values as input arguments therefore, the program output shows a fatal error as shown below.

Type 3: Strict Mode Scalar and Return Type Declaration.
In this strict type scalar declaration, the function ‘returnFloatValue’ accepts and returns a float value. When we are calling this function inside the print statement and passing a floating number value (56.98) as an argument, the PHP program executes fine.
Output: Since the return type and the input argument values are both float data type. Strict mode enforced declaration works fine giving the output as shown below.

Type 4: Strict Mode Scalar and Return Type Declaration that fails.
In this PHP program, the function ‘returnBoolValue’ accepts a Boolean value and returns a Boolean value and PHP script has strict mode scalar type declaration enforced. We call this function inside the print statement and passing an integer value 100 as shown below. This is strict mode data type violation therefore, a fatal error is thrown by the PHP parser.
Output: Since the input argument is Boolean type, but function is passing an integer value where strict mode is the scalar type declaration. Hence, the output below shows a fatal error thrown by the system.
Click Here for the Source Code of this Artice .

We must use strict mode scalar type when the PHP program requires strict data type to be enforced across various functions otherwise default coercive mode should be used to avoid fatal errors as shown in the previous examples. This completes the explanation on the scalar and return type declaration feature that is added to PHP 7.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -