Javatpoint Logo
Javatpoint Logo

PHP Type Casting and Conversion of an Object to an Object of other class

PHP type casting helps the developer to easily convert one type of value to another, or a given PHP class object can also be converted to object of a different class.

Program 1: PHP type casting to an integer

Output:

PHP Type Casting and Conversion of an Object to an Object of other class

In the above program, we have declared multiple variables a, b, c, d, e, f, g with multiple data types, and the main motive of the program is to convert different data types to int. We have used PHP inbuilt function var_dump () in order to show the data types of the declared variable. And, we have used data type casting to change the previous data type to int and then again used var_dump to display the changed data type

Program 2: PHP type casting to float

Output:

PHP Type Casting and Conversion of an Object to an Object of other class

Here in this program, we have declared multiple variables a, b, c, d, e, f, g with multiple data types, and the main motive of the program is to convert different data types to float. We have used PHP inbuilt function var_dump () to show the data types of the declared variable.

And we have used data type casting to change the previous data type to float and then again used var_dump to display the changed data type. Also, we can change the array, which will return 0 if its an empty array and 1 if the array contains any element.

Program 3: PHP type casting to string

Output:

PHP Type Casting and Conversion of an Object to an Object of other class

Here in this program we have declared multiple variables a, b, c, d, e, f, g with multiple data types and the main motive of the program is to convert different data types to string, we have used PHP inbuilt function var_dump () to show the data types of the declared variable

And we have used data type casting to change the previous data type to string and then again used var_dump to display the changed data type. Apart the basic data type we can also convert Boolean variables to string if the Boolean result is true the converted string will return 1 and if the Boolean value is false the converted string will return 0. Also, we can change array which will return a string.

Example 4: PHP type casting in array

Output:

PHP Type Casting and Conversion of an Object to an Object of other class

Here in this program, we have declared two variables, a, b, with the array data type, and the program's main motive is to show the conversion of the array. We have used PHP inbuilt function var_dump () to show the data types of the declared variable.

And we have used data type casting to change the previous data type and then again used var_dump to display the changed data type.

In the first part, we have used type casting to convert an array to a string.

And in the next part, we have converted the array to object with all the elements of the array converted to objects.

Example 5: PHP typecasting in the object by using for each loop and then initiating properties of first-class to the final class

Output:

PHP Type Casting and Conversion of an Object to an Object of other class

Here in this program, we have declared two classes, Mainclass and Secondary. In this program, we have used PHP type casting to convert objects of the main class to objects of the secondary class.


Next TopicPHP PEAR





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA