Home > Backend Development > PHP7 > How to enable strong type mode in php7

How to enable strong type mode in php7

醉折花枝作酒筹
Release: 2023-02-17 22:54:01
forward
2857 people have browsed it

This article will introduce to you how to enable strong type mode in php7. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to enable strong type mode in php7

We know that php is a weakly typed programming language, but php7 has changed and can support code to enable strong type mode. Good news.

php7 enables strong type mode, which is part of the reason why php7 has improved efficiency compared to previous versions. Let’s look at two examples first

First

<?php
function sum($a,$b):float
{
        return $a+$b;
}
var_dump(sum(1,2));
var_dump(sum(1,2.5));
?>
Copy after login

The output result is:

float(3) float(3.5)
Copy after login

Then add the strong type mode

<?php
define(strict_types=1);//注意这一句必须要放在第一行,而且顶格
function sum($a,$b):float
{
        return $a+$b;
}
var_dump(sum(1,2));
var_dump(sum(1,2.5));
?>
Copy after login

The output result is:

float(3) float(3.5)
Copy after login

It seems that there is no difference from here, because int->float type conversion is allowed .

Let’s look at another example:

function sum(int $a,int $b):float
{
        return $a+$b;
}
var_dump(sum(1,2));
var_dump(sum(1,2.5));
Copy after login

The output result is:

float(3) float(3)
Copy after login

This is because 2.5 is forced to be converted to int type, the value is 2, 1 2=3, There is nothing wrong with the result, but generally speaking, this implicit conversion is too difficult to understand and may not be the result we expected.

So we add the strong type mode to see the output result. The code is as follows:

<?php
declare(strict_types=1);
function sum(int $a,int $b):float
{
        return $a+$b;
}
var_dump(sum(1,2));
var_dump(sum(1,2.5));
?>
Copy after login

The output result is:

float(3)
Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, float given, called in /home/www/learn.php on line 8 and defined in /home/www/learn.php:3 Stack trace: #0 /home/www/learn.php(8): sum(1, 2.5) #1 {main} thrown in /home/www/learn.php on line 3
Copy after login

It can be seen here that the strong type mode is in effect. There is an error in the sum(1,2.5) sentence. 2.5 is not an int type, causing an error in the program.

If we encounter this situation, let us catch this error by catching an exception

The code is as follows:

<?php
declare(strict_types=1);
function sum(int $a,int $b):float
{
        return $a+$b;
}
try {
var_dump(sum(1,2));
var_dump(sum(1,2.5));
}
catch(TypeError $e) {
        echo &#39;Error:&#39;.$e->getMessage();
}
?>
Copy after login

The output result is:

float(3) 
Error:Argument 2 passed to sum() must be of the type integer, float given, called in /home/www/learn.php on line 9
Copy after login

Recommended learning: php video tutorial

The above is the detailed content of How to enable strong type mode in php7. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template