Open In App

How to check the Data Type and Value of a Variable in PHP ?

Last Updated : 19 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

To determine the data type and value of a variable in PHP, you can use the var_dump( ) function. Simply pass the variable as an argument to var_dump( ), and it will output both the data type and value of the variable.

Syntax:

$variable = "Hello";
var_dump($variable);

// Output: string(5) "Hello"

Alternatively, you can use the PHP gettype() function to retrieve only the data type of the variable without displaying its value.

$variable = 10;
$type = gettype($variable);
echo $type;

// Output: integer

Printing both datatype and value

This code assigns the string “Hello” to the variable ‘$variable’, determines its data type using ‘gettype()’, and outputs the type and value.

$variable = "Hello";
$type = gettype($variable);
echo "Data type: " . $type . ", Value: " . $variable;

// Output: Data type: string, Value: Hello

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads