What is the reason for casting in php?

I saw this on some posts:

$num = "5"; if(((int)$num) < 4){ ...} 

is there any reason to distinguish "5" as an int, or is it also good to say:

 if($num < 4){ ...} 

because I checked it with my code:

 echo $num + 4; //outputs 9 echo (int)$num + 4;//also outputs 9 

Update: My question about casting in general is just one or two examples above.

Update 2: immediately juggling manual php type

 <?php $foo = "0"; // $foo is string (ASCII 48) $foo += 2; // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is now a float (3.3) $foo = 5 + "10 Little Piggies"; // $foo is integer (15) $foo = 5 + "10 Small Pigs"; // $foo is integer (15) ?> 

^^^^ Why are these last two things happening?

+6
string casting php
source share
3 answers

In the cases that you mentioned, there is no serious reason indeed . This is due not only to the fact that PHP is a dynamically typed language, and the operators used are not type sensitive.

However, casting has many good uses. In the case of (int) you can use to ensure that you always use an integer during operations. In addition, by keeping ahead of time you save PHP from having to constantly introduce juggling later.

Edit due to editing question (rev4)

The last two points are due to the fact that PHP will try to force a string into an integer during a mathematical operation. Thus, it parses the string as a number. As soon as he cannot find a real integer, the found number is returned.

Basically, from the beginning of the line, find everything that matches the integer / floating format . Once something STOPS conforms to this format, return what you have. If the first character cannot match the format, return 0; .

For a better explanation, see: http://www.php.net/manual/en/language.types.integer.php#language.types.integer.casting

+7
source share

Everything that I read here sounds quite reasonable, so I will not rephrase it. Instead, I will give two examples of where I often use type casting in PHP:

  • Providing an API and attempting to return the correct types in different formats usually requires an explicit input of each return value. For example, if I return some mixed data array via XML_Serializer or XML-RPC, and I do not correctly use my ints / float, they will be returned as strings, which cause all the problems for people using strongly typed languages ​​trying to use the API. However, I can’t say how SOAP + WSDL will handle this because I have not messed it up.

  • Casting return values ​​to arrays in cases when the library (or our code) returns either an array or zero, and we cannot / do not want to change it. This is usually solely to prevent the warnings you receive when a non-array is passed to an array structure or method.

+2
source share

You do not need to throw when performing arithmetic operations, because PHP will do this implicitly. I have never had a case where an implicit default cast was not what I wanted. In fact, casting is rarely applicable in PHP.

The listing that I do most often explicitly converts the string to an integer (or float) into an assignment when I know that the variable will be used in many mathematical operations.

It is useful to read:

+1
source share

All Articles