PHP Data Types
- Previous Page PHP Echo / Print
- Next Page PHP String Functions
Strings, integers, floating-point numbers, logic, arrays, objects, NULL.
PHP Strings
Strings are sequences of characters, such as "Hello world!"
Strings can be any text within quotes. You can use single quotes or double quotes:
Example
<?php $x = "Hello world!"; echo $x; echo "<br>"; $x = 'Hello world!'; echo $x; ?>
PHP Integers
Integers are numbers without decimals.
Integer rules:
- Integers must have at least one digit (0-9)
- Integers cannot contain commas or spaces
- Integers cannot have a decimal point
- Integers can be positive or negative
- Integers can be specified in three formats: decimal, hexadecimal (prefix is 0x), or octal (prefix is 0)
In the following example, we will test different numbers. PHP var_dump() will return the data type and value of the variable:
Example
<?php $x = 5985; var_dump($x); echo "<br>"; $x = -345; // Negative number var_dump($x); echo "<br>"; $x = 0x8C; // Hexadecimal number var_dump($x); echo "<br>"; $x = 047; // Octal number var_dump($x); ?>
PHP Floating-point numbers
Floating-point numbers are numbers with a decimal point or in exponential form.
In the following example, we will test different numbers. PHP var_dump() will return the data type and value of the variable:
Example
<?php $x = 10.365; var_dump($x); echo "<br>"; $x = 2.4e3; var_dump($x); echo "<br>"; $x = 8E-5; var_dump($x); ?>
PHP Logic
Logic is true or false.
$x=true; $y=false;
Logic is commonly used for conditional tests. You will learn more about conditional tests in the later chapters of this tutorial.
PHP Arrays
An array stores multiple values in a single variable.
In the following example, we will test different arrays. PHP var_dump() will return the data type and value of the variable:
Example
<?php $cars=array("Volvo","BMW","SAAB"); var_dump($cars); ?>
You will learn more about arrays in later chapters of this tutorial.
PHP Objects
An object is a data type that stores data and information about how to handle the data.
In PHP, objects must be declared explicitly.
First, we must declare the class of the object. For this, we use the class keyword. A class is a structure that contains properties and methods.
Then we define data types in the object class and use this data type in the instance of the class:
Example
<?php class Car { var $color; function Car($color="green") { $this->color = $color; } function what_color() { return $this->color; } } ?>
You will learn more about objects in later chapters of this tutorial.
PHP NULL Value
A special NULL value indicates that a variable has no value. NULL is the only possible value of the data type NULL.
NULL value indicates whether a variable is empty. It is also used to distinguish between empty strings and null database values.
You can clear a variable by setting its value to NULL:
Example
<?php $x="Hello world!"; $x=null; var_dump($x); ?>
- Previous Page PHP Echo / Print
- Next Page PHP String Functions