Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP Data Types

Variables can store data of various types, and different data types serve different purposes.

PHP supports the following data types:

  • String
  • Integer
  • Float (floating-point numbers, also known as double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

Getting the Data Type

You can determine the data type of any object by using the var_dump() function.

Example

The var_dump() function provides both the data type and the value.

$x = 5;
var_dump($x);

PHP NULL Value

NULL is a special data type that can hold only one value: NULL.

A variable of data type NULL is one that has no value assigned to it.

Tip: If a variable is created without an assigned value, it is automatically given a value of NULL.

Variables can also be cleared by setting their value to NULL.

Example

$x = "Hello world!";
$x = null;
var_dump($x);

Change Data Type

If you assign an integer value to a variable, its type will automatically be set to integer.

If you then assign a string to the same variable, its type will change to string.

Example

$x = 5;
var_dump($x);

$x
= "Hello";
var_dump($x);

If you want to change the data type of an existing variable without altering its value, you can use casting.

Casting enables you to modify the data type of variables.

Example

$x = 5;
$x = (string) $x;
var_dump($x);

PHP Resource

The special resource type is not a true data type; it refers to a reference to functions and resources outside of PHP.

A typical example of the resource data type is a database call.