Skip to content

Types and Type Conversions in PHP

Introduction to Data Types in PHP

PHP is a loosely typed language, meaning variables do not have a fixed data type. PHP automatically determines the type of a variable based on its assigned value. However, PHP also provides mechanisms for explicit type conversion when needed.


1. Data Types in PHP

PHP supports eight primary data types, divided into three categories:

1.1 Scalar (Single Value) Data Types

These types store a single value at a time.

Data TypeDescriptionExample
IntegerWhole numbers (positive or negative)10, -50, 1000
Float (Double)Decimal numbers3.14, -0.99, 10.5E3
StringSequence of characters“Hello, PHP!”, ‘123’
BooleanRepresents true or falsetrue, false

1.2 Compound (Multiple Values) Data Types

These types can store multiple values at once.

Data TypeDescriptionExample
ArrayStores multiple values in a single variable[“Apple”, “Banana”, “Cherry”]
ObjectRepresents an instance of a class$obj = new Car();

1.3 Special Data Types

These types are used in specific scenarios.

Data TypeDescriptionExample
NULLRepresents a variable with no value$var = NULL;
ResourceHolds external resources like database connections$file = fopen(“file.txt”, “r”);

2. Type Checking in PHP

PHP provides functions to check a variable’s type.

Example: Checking Data Types

<?php

$var1 = 10; 

$var2 = 3.14; 

$var3 = “Hello”; 

$var4 = true; 

echo gettype($var1); // Output: integer 

echo gettype($var2); // Output: double (float) 

echo gettype($var3); // Output: string 

echo gettype($var4); // Output: boolean 

?>

PHP Functions for Type Checking

FunctionDescriptionExample
is_int()Checks if a variable is an integeris_int(100) → true
is_float()Checks if a variable is a floatis_float(3.14) → true
is_string()Checks if a variable is a stringis_string(“Hello”) → true
is_bool()Checks if a variable is a booleanis_bool(true) → true
is_null()Checks if a variable is NULLis_null(NULL) → true
is_array()Checks if a variable is an arrayis_array([1,2,3]) → true
is_object()Checks if a variable is an objectis_object(new Car()) → true

3. Type Conversion in PHP

PHP allows automatic (implicit) type conversion and manual (explicit) type conversion.

3.1 Implicit Type Conversion (Type Juggling)

PHP automatically converts data types when necessary.

Example: Implicit Type Conversion

<?php

$x = “10” + 5; // “10” (string) is converted to an integer

echo $x;  // Output: 15

?>

  • Here, PHP converts “10” (string) into 10 (integer) before performing the addition.

More Examples of Implicit Conversion

<?php

$y = “5.5” * 2;  // “5.5” (string) becomes 5.5 (float)

echo $y;  // Output: 11.0

$z = true + 1;  // true (1) + 1 = 2

echo $z;  // Output: 2

?>


3.2 Explicit Type Conversion (Type Casting)

When needed, you can manually convert a variable’s type using type casting or conversion functions.

Type Casting in PHP

You can convert a variable to a different type by prefixing it with the desired type in parentheses.

<?php

$var = “100”;

$int_var = (int)$var;  // Convert string to integer

echo $int_var;  // Output: 100

?>

Available Type Casts:

Cast SyntaxConverts To
(int) $var or (integer) $varInteger
(float) $var or (double) $var or (real) $varFloat
(bool) $var or (boolean) $varBoolean
(string) $varString
(array) $varArray
(object) $varObject
(unset) $varNULL

3.3 Type Conversion Using Functions

PHP provides functions for explicit type conversion.

FunctionDescriptionExample
intval()Converts a value to an integerintval(“123”) → 123
floatval()Converts a value to a floatfloatval(“3.14”) → 3.14
strval()Converts a value to a stringstrval(100) → “100”

Example: Conversion Functions

<?php

$num = “123.45”;

$int_num = intval($num);   // Convert to integer

$float_num = floatval($num); // Convert to float

$string_num = strval($num); // Convert to string

echo $int_num;  // Output: 123

echo $float_num;  // Output: 123.45

echo $string_num;  // Output: “123.45”

?>


4. Automatic Type Conversion in Expressions

PHP dynamically converts types in expressions depending on the operation.

Example 1: String to Integer Conversion

<?php

$result = “50” + 20; // “50” is converted to an integer

echo $result;  // Output: 70

?>

Example 2: Boolean to Integer Conversion

<?php

$bool = true + 5; // true is converted to 1

echo $bool;  // Output: 6

?>


5. Converting Arrays and Objects

PHP allows conversion between arrays and objects using casting and type functions.

Converting an Array to an Object

<?php

$array = [“name” => “John”, “age” => 30];

$object = (object)$array;

echo $object->name;  // Output: John

?>

Converting an Object to an Array

<?php

class Person {

    public $name = “John”;

    public $age = 30;

}

$person = new Person();

$array = (array)$person;

print_r($array); 

// Output: Array ( [name] => John [age] => 30 )

?>


6. Summary of PHP Type Conversion

  • PHP automatically converts types when needed (Implicit Type Conversion).
  • Developers can manually convert types using type casting or conversion functions.
  • PHP allows dynamic type conversion in expressions for flexible coding.

Conclusion

Understanding data types and type conversion in PHP is crucial for handling variables, performing calculations, and ensuring data integrity. Using type checking, type casting, and conversion functions, developers can write efficient and error-free code.