Skip to content

Working with Data Types in PHP

  • PHP is a dynamically typed language, meaning you don’t need to declare variable types explicitly ==> PHP automatically determines the type based on the value assigned.

Sequences of characters enclosed in quotes:

$singleQuoted = 'Hello World';
$doubleQuoted = "Hello World";
$name = "John";
$greeting = "Hello, $name!"; // Variable interpolation with double quotes
// Heredoc for multi-line strings
$html = <<<HTML
<div>
<h1>Welcome, $name!</h1>
</div>
HTML;

Whole numbers (positive or negative):

$positive = 42;
$negative = -17;
$octal = 0755; // Octal notation
$hexadecimal = 0xFF; // Hexadecimal notation
$binary = 0b1010; // Binary notation (PHP 5.4+)

Numbers with decimal points:

$price = 19.99;
$scientific = 1.2e3; // Scientific notation (1200)
$negative = -3.14;

True or false values:

$isActive = true;
$isComplete = false;
// Values that evaluate to false
$false1 = 0; // Integer zero
$false2 = 0.0; // Float zero
$false3 = ""; // Empty string
$false4 = "0"; // String zero
$false5 = null; // NULL value
$false6 = array(); // Empty array

Collection of values:

// Indexed array
$fruits = array("apple", "banana", "orange");
$colors = ["red", "green", "blue"]; // Short syntax (PHP 5.4+)
// Associative array
$person = array(
"name" => "John",
"age" => 30,
"city" => "New York"
);
// Mixed array
$mixed = [
0 => "first",
"key" => "value",
1 => 42
];

Instances of classes:

class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$person = new Person("John", 30);

Represents no value:

$empty = null;
$undefined = NULL; // Case insensitive
// Variables become NULL when:
unset($someVariable); // Explicitly unset
$notAssigned; // Never assigned (generates notice)

Special variables holding references to external resources:

$file = fopen("data.txt", "r"); // File resource
$connection = mysqli_connect("localhost", "user", "pass", "db"); // Database resource

$value = "Hello";
echo gettype($value); // "string"
echo var_dump($value); // string(5) "Hello"
echo print_r($value, true); // Hello
// Type checking functions
var_dump(is_string($value)); // bool(true)
var_dump(is_int($value)); // bool(false)
var_dump(is_array($value)); // bool(false)
var_dump(is_null($value)); // bool(false)
$name = "John";
var_dump(isset($name)); // bool(true)
var_dump(isset($undefined)); // bool(false)
var_dump(empty($name)); // bool(false)
var_dump(empty("")); // bool(true)
// Check if variable is set and not null
if (isset($name) && $name !== null) {
echo "Variable exists and has value";
}


PHP automatically converts types when needed in expressions:

$str = "123";
$num = $str + 0; // $num becomes integer 123
$result = $str * 2; // $result becomes integer 246
// Leading numeric strings
$price = "19.99 dollars";
$tax = $price * 0.1; // $tax becomes float 1.999
// Non-numeric strings become 0
$text = "hello";
$result = $text + 5; // $result becomes integer 5
$number = 123;
$string = $number . " items"; // "123 items"
$concat = "Price: $" . 19.99; // "Price: $19.99"
// Converting to boolean in conditions
$value = "0";
if ($value) { // false - string "0" is falsy
echo "Truthy";
} else {
echo "Falsy";
}
// Explicit boolean conversion
$bool = (bool)"hello"; // true
$bool = (bool)""; // false
$bool = (bool)0; // false
$bool = (bool)1; // true
$array = [1, 2, 3];
$count = count($array); // Integer 3
$string = implode(",", $array); // "1,2,3"
// Object to array
$obj = new stdClass();
$obj->name = "John";
$array = (array)$obj; // ["name" => "John"]

$value = "123.45";
$int = (int)$value; // 123
$float = (float)$value; // 123.45
$string = (string)123; // "123"
$bool = (bool)$value; // true
$array = (array)$value; // ["123.45"]
$object = (object)$value; // stdClass with scalar property
$value = "123.45";
// Convert to integer
$int1 = intval($value); // 123
$int2 = (int)$value; // 123
$int3 = filter_var($value, FILTER_VALIDATE_INT); // false (not pure int)
// Convert to float
$float1 = floatval($value); // 123.45
$float2 = (float)$value; // 123.45
// Convert to string
$string1 = strval(123); // "123"
$string2 = (string)123; // "123"
// Convert to boolean
$bool1 = boolval($value); // true
$bool2 = (bool)$value; // true
function safeIntConvert($value, $default = 0) {
if (is_numeric($value)) {
return (int)$value;
}
return $default;
}
$userInput = "abc123";
$number = safeIntConvert($userInput, 0); // Returns 0