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 determines the type based on the value assigned.


Sequences of characters enclosed in quotes.

$single = 'Hello World';
$double = "Hello World";
$name = "John";
$greeting = "Hello, $name!"; // Variable interpolation (double quotes only)
// 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
$hex = 0xFF; // Hexadecimal
$binary = 0b1010; // Binary

Numbers with decimal points.

$price = 19.99;
$scientific = 1.2e3; // 1200

True or false values.

$isActive = true;
$isComplete = false;
// These values evaluate to false:
// 0, 0.0, "", "0", null, []

Collections of values.

// Indexed array
$colors = ["red", "green", "blue"];
// Associative array
$person = [
"name" => "John",
"age" => 30,
"city" => "New York"
];

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;
// A variable becomes null when:
unset($someVariable); // Explicitly unset

References to external resources like files or database connections.

$file = fopen("data.txt", "r");
$db = mysqli_connect("localhost", "user", "pass", "db");

$value = "Hello";
gettype($value); // "string"
var_dump($value); // string(5) "Hello"
is_string($value); // true
is_int($value); // false
is_array($value); // false
is_null($value); // false
is_numeric("123"); // true
$name = "John";
isset($name); // true - exists and not null
isset($undefined); // false
empty($name); // false
empty(""); // true


Type juggling (also called type coercion) happens when PHP automatically converts a value from one type to another during an operation. For example, if you add a number to a string that looks like a number, PHP converts the string to a number first, then performs the addition. This happens behind the scenes without you writing any conversion code.

// String to number
$str = "123";
$result = $str + 10; // 133 (integer)
$price = "19.99 dollars";
$tax = $price * 0.1; // 1.999 (float, reads leading number)
$text = "hello";
$result = $text + 5; // 5 (non-numeric string becomes 0)
// Number to string
$number = 123;
$string = $number . " items"; // "123 items"
// Boolean context
if ("0") { } // false - string "0" is falsy
if ("hello") { } // true

$value = "123.45";
(int)$value; // 123
(float)$value; // 123.45
(string)123; // "123"
(bool)$value; // true
(array)$value; // ["123.45"]
$value = "123.45";
intval($value); // 123
floatval($value); // 123.45
strval(123); // "123"
boolval($value); // true
$obj = new stdClass();
$obj->name = "John";
$array = (array)$obj; // ["name" => "John"]

When dealing with user input, validate before converting.

function safeIntConvert($value, $default = 0) {
if (is_numeric($value)) {
return (int)$value;
}
return $default;
}
$userInput = "abc123";
$number = safeIntConvert($userInput, 0); // Returns 0