PHP Variable Handling
Managing and inspecting variables in PHP
🔧 What is Variable Handling?
Variable handling functions help you check, test, and manipulate variables. These functions let you determine variable types, check if variables exist, and convert between different data types safely.
<?php
// Check variable type
$name = "John";
echo gettype($name); // string
?>
Output:
string
Variable Functions Overview
Type Checking
Identify variable data types
Existence Check
Verify if variables are set
Type Conversion
Convert between data types
Variable Info
Get detailed variable information
🔹 Checking Variable Types
Use type-checking functions to verify what kind of data a variable contains. This helps prevent errors and ensures your code handles data correctly.
<?php
$number = 42;
$text = "Hello";
$list = [1, 2, 3];
// Check if integer
echo is_int($number) ? "Yes, it's an integer" : "No";
// Check if string
echo "<br>" . (is_string($text) ? "Yes, it's a string" : "No");
// Check if array
echo "<br>" . (is_array($list) ? "Yes, it's an array" : "No");
?>
Output:
Yes, it's an integer
Yes, it's a string
Yes, it's an array
🔹 isset() and empty()
Check if variables are set and contain values. isset() checks if a variable exists, while empty() checks if it's empty or false.
<?php
$name = "John";
$age = 0;
$city = "";
// Check if variable is set
echo isset($name) ? "Name is set" : "Not set";
// Check if variable is empty
echo "<br>" . (empty($city) ? "City is empty" : "City has value");
// Zero is considered empty
echo "<br>" . (empty($age) ? "Age is empty" : "Age has value");
?>
Output:
Name is set
City is empty
Age is empty
🔹 Getting Variable Type
Use gettype() to find out what type of data a variable holds. This is useful for debugging and understanding your data structures.
<?php
$var1 = 100;
$var2 = "Hello";
$var3 = 3.14;
$var4 = true;
$var5 = null;
echo "var1 is: " . gettype($var1);
echo "<br>var2 is: " . gettype($var2);
echo "<br>var3 is: " . gettype($var3);
echo "<br>var4 is: " . gettype($var4);
echo "<br>var5 is: " . gettype($var5);
?>
Output:
var1 is: integer
var2 is: string
var3 is: double
var4 is: boolean
var5 is: NULL
🔹 Type Conversion
Convert variables from one type to another using conversion functions. This ensures data is in the correct format for operations and comparisons.
<?php
$text = "123";
$decimal = "45.67";
// Convert string to integer
$number = intval($text);
echo "Integer: " . $number . " (type: " . gettype($number) . ")";
// Convert string to float
$float = floatval($decimal);
echo "<br>Float: " . $float . " (type: " . gettype($float) . ")";
// Convert number to string
$str = strval(100);
echo "<br>String: " . $str . " (type: " . gettype($str) . ")";
?>
Output:
Integer: 123 (type: integer)
Float: 45.67 (type: double)
String: 100 (type: string)
🔹 var_dump() and print_r()
Display detailed information about variables for debugging. var_dump() shows type and value, while print_r() displays human-readable information about arrays and objects.
<?php
$data = ["name" => "John", "age" => 30];
// Detailed variable information
echo "var_dump output:<br>";
var_dump($data);
// Human-readable format
echo "<br><br>print_r output:<br>";
print_r($data);
?>
Output:
var_dump output:
array(2) { ["name"]=> string(4) "John" ["age"]=> int(30) }
print_r output:
Array ( [name] => John [age] => 30 )
🔹 is_null() and unset()
Check for null values and remove variables from memory. is_null() tests if a variable is null, while unset() destroys the variable completely.
<?php
$value = null;
$name = "John";
// Check if null
echo is_null($value) ? "Value is null" : "Value is not null";
// Unset a variable
unset($name);
echo "<br>" . (isset($name) ? "Name exists" : "Name was unset");
?>
Output:
Value is null
Name was unset
🔹 is_numeric() and is_scalar()
Check if variables contain numeric values or scalar types. These functions help validate input and ensure data is in the expected format.
<?php
$num1 = 123;
$num2 = "456";
$text = "hello";
// Check if numeric (number or numeric string)
echo is_numeric($num1) ? "num1 is numeric" : "Not numeric";
echo "<br>" . (is_numeric($num2) ? "num2 is numeric" : "Not numeric");
echo "<br>" . (is_numeric($text) ? "text is numeric" : "Not numeric");
// Check if scalar (int, float, string, or bool)
echo "<br>" . (is_scalar($num1) ? "num1 is scalar" : "Not scalar");
?>
Output:
num1 is numeric
num2 is numeric
Not numeric
num1 is scalar