Changing the type of variables in PHP

Ways in which a PHP variable can change its type.

In the previous article we began to explain in this language. We already discussed that PHP is dynamically typed, but there are a few things you may need to do on a day-to-day basis that you should know to cover the fundamental knowledge of variables and data types in PHP.

PHP does not require that we indicate the type that a variable is going to contain, but it deduces it from the value that we assign to the variable. Likewise, it is in charge of automatically updating the type of the variable each time we assign a new value to it. This is basically what is called “dynamic typing” or “weak typing”, a feature not only of PHP, but of many other languages ​​such as Javascript.

Therefore, to change the type of a variable we simply assign it a value with a new type.

$string=”this is a string”; $string = 34 //The variable $string changed its type Note: In this case, the change of variables to type Array is excluded because the syntax may be ambiguous when expressing that code, that is, it may be the case that a line of code can mean two things. $a = “1”; //$a is a string $a = “f”; //Are we editing the index of the string or forcing it to array?

In this article we will see two possible types of variable type alteration, beyond the one that PHP does with the behavior derived from its dynamic typing. This operation is commonly known as “Forcing”.

Forced

Varying the type of data that a variable contains over time is something that is not always advisable, because if we are not sure if a variable contains data of one type or another, sometimes the results obtained may not be what we expected.

See also  inertia.js

To avoid problems, on many occasions it can be useful to force a variable to a specific type, explicitly, which will allow us to know that when the flow of the program reaches a given point, that variable will have the expected data type. . In PHP there are several ways to force a variable to a type.

Set the type with settype()

We can force a variable to change type with the settype() function.

settype($variable,”new_type”);

the setType() function updates the type of $variable to “new_type” and returns a boolean indicating whether or not the conversion was successful.

Between “new_type” we have:

  • “integer”
  • “double”
  • string
  • “array”
  • object

variable casting

There is another way to perform a forcing, so that a variable behaves as a certain type. Now we are going to see another forcing mechanism that is similar to that of other languages ​​such as C or Java.

$variable = “23”; $variable = (int) $variable;

The allowed forces are:

  • (int), (integer) – force to integer (integer)
  • (real), (double), (float) – force a number with decimal places (floating point)
  • (string) – force to string (string)
  • (array) – force to array (array)
  • (object) – force to object (object)
  • (unser) – force to null
  • (binary) – force to “binary string”

conclusion

If you are just starting out with PHP and programming in general, perhaps this topic of type changing and forcing may seem like a bit of advanced information or without a clear application. If so, don’t worry too much for now, but keep in mind that you as a programmer are able to change the types of variables, so that your programs do exactly what you want.

See also  open_basedir problem on servers with Plesk

When there is a type inconsistency, PHP always tries to do what is most appropriate with the code it executes, but the solution it takes is not always the one you might think. In those cases, the forcing will be really important. Without a doubt, when you have more experience with the language, these situations will appear.

Loading Facebook Comments ...
Loading Disqus Comments ...