Changing the type

Changing the type

It is usually not necessary to determine the type of the variable – PHP itself is determined, depending on the context.
When assigning a variable a new value, the previous value is obviously blurring. In this case the variable type is determined again view website.
However, if you need to change the type or PHP incorrectly identifies the type, it can be done using a projection (cast – the effect is a one-off), or using settype (lasting effect).
Type Casting is done by specifying a new type in parentheses before the variable or the value of the type you want to change.


<? php
$ Integer = 6;

$ real = (real) $ Integer;

Permitted types of Cast Designer

• (int), (integer) – roll up to the integer
• (real), (double), (float) – roll up to the real type
• (string) – to roll up over
• (array) – roll up to the plate
• (object) – roll up to the object

Way permanent, it is to use the settype. This function takes two arguments. The first is the name of the variable to determine the type, and the second string specifying a new type of variable.

Acceptable function arguments settype

  • „integer”
  • „double”
  • „string”
  • „array”
  • „object”

The function returns „true” if everything went well. Otherwise, the return value is „false”.

<? php

$ var = 7.8;

echo „$ variable <br>”;                                      // The value displayed is „7.8”

settype ($ variable „integer”);

echo „$ variable <br>”;                                             // The value displayed is „7”

Close Menu