settype and gettype Function in PHP with Examples

Description:

I will explain two functions gettype and settype in php these two functions are related to the data type of variables in PHP.

gettype funtion:

first is the gettype function gettype function returns the data type of a variable which is passed in the argument of a function returned type can be any valid data type like integer double string object Boolean.

Syntax: String gettype (anytype of variable)

it returns the string value.



example: how to use gettype function in PHP:

gettype and settype function in php

In the above program, I  taken four variables are “a” that is value is 10 so here the type is integer b variable the value is string and c variable the value is of type float or you can say double and variable d the value is of type Boolean. And then I used the echo function to print all these data types on the webpage as you can in the above figure.

As you know the PHP is a loosely typed language so whenever we are declaring the variable we are not giving the data type the value that is assigned in the variable according to that the data type will be taken automatically as you can see in the program I define variable “a” and assign the value 10 which is an integer. The PHP language considered automatically it is an integer value. similarly for b PHP language automatically get its data type and print on the webpage, and similarly for c and d.


settype function:

settype function is used to set the data type of any variable any valid data type any valid PHP data type can be sent like integer Boolean, string, double, resource, object, array, etc. it returns the boolean value.

syntax: bool(anytype var, string type)

in an argument there are two arguments first is the variable for which you want to set the new type and the second is the type that you want to set for that variable so here it returns the boolean value  it returns true on the successful operation and it returns false on the failure.

Example: how to use settype function in PHP:

gettype and settype function in php

the first is variable a value which is a string for variable a and you will get a boolean for variable b. now using settype function you are able to change the data type of any variable or you are able to set the new data type of any variable as you can see in the above program line number 6 i am calling the settype function the first argument is the variable a whose original data type is string and I changed it to integer using settype function. Like for b as you can see in the above program line number 7 I am calling settype function the first argument is the variable b whose original data type is Boolean and I changed it to a string. so it will be written integer when you will call gettype function as you can see in the above program line 8 and 9 for variable a and now the new data type of variable b is a string so whenever you will call gettype function for variable b it will return string.


related Article:

PHP variables and built-in function

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: