settype and gettype Functions in PHP with Examples

(Last Updated On: April 2, 2021)

Description:

settype and gettype functions– I will explain two functions gettype and settype in php. These two functions are related with the data type of variables in php. 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 is it. It is returning the data type of a variable.


Now this is a simple program of gettype() function:

settype and gettype

 

In the above program, I  chose four variables are “a” that is value is 10 so here the type is integer b variable the value is string 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 used the echo function to print all these data type on 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 integer. The php language considered automatically it is an integer value. similarly for b php language automatically get its data type and print on webpage, and similarly  for c and d.



next is 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)

and in argument there are two arguments first is the variable for which you want to set the new type and 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

now this is the example

 

settype and gettype

the first is variable a value which is sstring 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 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 string. so it will 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 string so whenever you will call gettype function for variable b it will return string.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply