Let's write a safe program using PHP type declarations!

Hello! I'm Teshima, who joined the Development Department just recently.
In this article, I'd like to introduce the type declarations updated since PHP 7.0.

I'll get to that in a minute, but have you ever had an error when coding because the type of data you assigned to a variable was different?

Recently, PHP has been adopted for large-scale system development, and when the amount of code becomes enormous, unexpected errors may occur due to type problems.
For those times, you can avoid errors by coding with types in mind!

So in this post, we're going to talk about type declarations!

    table of contents

About variables in PHP
About the function about the data type of PHP
- Declaring types for function arguments
- Declaring a type for a function return value
- Extra (Comparison operator (= = =))

    About variables in PHP

In PHP, if you assign an integer to a variable, it will be of type int, if you assign a string, it will be of type string, and the type of the variable is automatically determined by implicit consent.
The feature is that you don't need to define a type when you create a variable.
On the other hand, other languages, such as Java and C, require variables to have type declarations as well.

// Example
  $string = "abc"; //Substitute a string
  var_dump($string); //string(3) "abc" become string type
  $int = 123; //assign an integer
  var_dump($int); //int(123) become int type

  //In Java
  String string; //declare the variable string of type string
  int integer; //declare the variable integer of type int

  // Cannot be assigned unless it is the declared data type

The above example shows that PHP allows you to code without being aware of types.
However, in this case, it is not known which type of data is contained in the variable, and unexpected bugs may occur.

    About PHP's Data Type Features

Declaring types for function arguments

// A program that doubles the argument and returns it
function doubleInt(int $int){
return $int * 2;
}
echo doubleInt(10); //printed as 20
echo doubleInt("abc"); //outputs an error

An error occurs when a data type other than numeric (int) is passed by specifying an argument to a function.
It also allows you to organize your functions in a simple way without having to write conversion code in the function.
Even in the unlikely event that a different person is given the program, you can rest assured that an error will occur and the program will terminate.

Declaring a type for a function return value

This section describes the case of a program that outputs a string as the return value even though the return value of the function introduced in 1 above is specified as int type.

function doubleInt(int $int): int{
return "Tatsuno Information System";
}
echo doubleInt(10); //Error.

In a large system development, the code in the function also becomes more complex.
You can avoid unexpected bugs by specifying the type of the return value to ensure that the processing is done correctly and does not result in an abnormal return value.

Since PHP7.4, type declaration is available for properties. If you are interested, please check the official manual.

Extra (Comparison operator (= = =))

This is something I have experienced myself in the past, so I hope you find it helpful.

The difference between the comparison operators (= =) and (= = =)

In the case of (==), even if the comparison target is a different data type, type conversion is automatically performed and comparison is executed.
On the other hand, in the case of (= = =), comparison execution is performed without changing the type.

The official PHP manual describes the
(= =) is described as a strict comparison and (= =) is described as a loose comparison.

//Example.
//comparison by number
var_dump(12345 == '12345'); //bool(true) unexpected but sometimes useful
var_dump(12345 === '12345'); //bool(false)

//comparison with numbers + strings
var_dump(12345 == '12345tatsuno'); //bool(true) not expected
var_dump(12345 === '12345tatsuno'); //bool(false)

var_dump(12345 == '12345abcde'); //bool(true) unexpected
var_dump(12345 === '12345abcde'); //bool(false)

var_dump('tatsuno' == 0); //bool(true) unexpected
var_dump('tatsuno' === 0); //bool(false)

When comparing numeric values and strings with *(==), if either value contains a numeric value, the string is converted to a numeric value before the comparison is performed.
The result (12345 == '12345tatsuno') becomes (12345 == 12345), which is true.
Also, if ('tatsuno' == 0), then (null == 0), which is also true.

This kind of comparison with the (==) operator can lead to unexpected bugs.
Basically, you can make your program safe by performing comparisons with the (==) operator.

    summary

How did it go?
I hope this will be helpful for those who usually code with type conversions without thinking (although I think there are not many...) and those who are going to learn programming.
Let's code with types in mind so we can write safer programs!

That's all I have to say, thank you!

en_USEnglish