Working with Data Types in PHP
Data Types
Section titled “Data Types”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.
Scalar Types
Section titled “Scalar Types”String
Section titled “String”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;Integer
Section titled “Integer”Whole numbers (positive or negative).
$positive = 42;$negative = -17;$octal = 0755; // Octal$hex = 0xFF; // Hexadecimal$binary = 0b1010; // BinaryNumbers with decimal points.
$price = 19.99;$scientific = 1.2e3; // 1200Boolean
Section titled “Boolean”True or false values.
$isActive = true;$isComplete = false;
// These values evaluate to false:// 0, 0.0, "", "0", null, []Compound Types
Section titled “Compound Types”Collections of values.
// Indexed array$colors = ["red", "green", "blue"];
// Associative array$person = [ "name" => "John", "age" => 30, "city" => "New York"];Object
Section titled “Object”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);Special Types
Section titled “Special Types”Represents no value.
$empty = null;
// A variable becomes null when:unset($someVariable); // Explicitly unsetResource
Section titled “Resource”References to external resources like files or database connections.
$file = fopen("data.txt", "r");$db = mysqli_connect("localhost", "user", "pass", "db");Type Checking
Section titled “Type Checking”Getting Variable Type
Section titled “Getting Variable Type”$value = "Hello";
gettype($value); // "string"var_dump($value); // string(5) "Hello"Type Checking Functions
Section titled “Type Checking Functions”is_string($value); // trueis_int($value); // falseis_array($value); // falseis_null($value); // falseis_numeric("123"); // trueChecking if Variable Exists
Section titled “Checking if Variable Exists”$name = "John";
isset($name); // true - exists and not nullisset($undefined); // falseempty($name); // falseempty(""); // trueType Conversion
Section titled “Type Conversion”Type Juggling (Implicit)
Section titled “Type Juggling (Implicit)”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 contextif ("0") { } // false - string "0" is falsyif ("hello") { } // trueType Casting (Explicit)
Section titled “Type Casting (Explicit)”Cast Operators
Section titled “Cast Operators”$value = "123.45";
(int)$value; // 123(float)$value; // 123.45(string)123; // "123"(bool)$value; // true(array)$value; // ["123.45"]Conversion Functions
Section titled “Conversion Functions”$value = "123.45";
intval($value); // 123floatval($value); // 123.45strval(123); // "123"boolval($value); // trueObject to Array
Section titled “Object to Array”$obj = new stdClass();$obj->name = "John";$array = (array)$obj; // ["name" => "John"]Safe Type Conversion
Section titled “Safe Type Conversion”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