Tuesday, January 23, 2018

Changing & Casting Type PHP

Changing by casting 

You can also cause a variable’s value to be treated as a specific type using a technique known as type casting. This involves placing the name of the desired data type in parentheses before the variable’s name. Note that the variable itself remains unaffected; this is in contrast to settype(), which changes the variable’s type.

In the following example, a variable’s value is cast to various different types at the time that the value is displayed:

Note that $test_var’s type isn’t changed at any point; it remains a floating-point variable, containing the value 8.23, at all times. All that changes is the type of the data that’s passed to the echo statement.

Here’s the full list of casts that you can use in PHP:-

Function                                     Description

(int) value or (integer) value  Returns value                                                   cast to an integer

(float) value                             Returns value           
                                                   cast to a float

(string) value                            Returns value                                                      cast to a string

(bool) value or (boolean) value   Returns                                                                  value cast                                                            to a Boolean

(array) value                              Returns value                                                      cast to an array

(object) value                            Returns value                                                    cast to an object

You can also cast a value to an integer, floating-point, or string value using three PHP functions:

Function                                 Description

intval( value )                          Returns value                                                     cast to an integer

floatval( value )                        Returns value                                                          cast to a float

strval( value )                            Returns value                                                        cast to a string

By the way, intval() has another use: converting from a non–base-10 integer to a base-10 integer. To use this feature, pass intval() a string representation of the non–base-10 integer, followed by the base of the integer. For example, intval( "11", 5 ) returns a value of 6 (the base-5 number 11 converted to a decimal number).

Why would you want to change a variable’s type with settype(), or change a value’s type with casting? Most of the time, PHP’s loose typing handles type conversion for you automatically, depending on the context in which you use variables and values. However, forcing a variable to be of a certain type is useful for security reasons; if you’re expecting to pass a user-entered integer value to a database, it’s a good idea to cast the value to an integer, just to make sure the user really did enter an integer. Likewise, if you’re passing data to another program, and that program expects the data to be in string format, you can cast the value to a string before you pass it.

                  Essentially, use explicit casting or settype() whenever you want to be absolutely sure that a variable contains data of a certain type. Read More


Please comment and follow this site blog inbox