WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 Data Types



PHP 5 Data Types

String (String), Integer (Float), Boolean (Boolean), Array (Object), NULL (null value).


PHP String

A string is a sequence of characters, like "Hello world!".

You can put any text in single and double quotes:

Examples

<?php $x = "Hello world!"; echo $x; echo "<br>"; $x = 'Hello world!'; echo $x; ?>


PHP Integer

An integer is a number without decimals.

Integer rule:

  • Integers must have at least one number (0-9)
  • Integers cannot contain commas or spaces
  • Integers have no decimal point
  • Integer can be positive or negative
  • Integers can be specified in three formats: decimal, Hex (prefixed with 0x) or octal (prefixed with 0).

In the following examples we will test different numbers.

PHP var_dump () The function returns the data type and value of the variable:

Example

Examples

<?php $x = 5985; var_dump($x); echo "<br>"; $x = -345; // negative number var_dump($x); echo "<br>"; $x = 0x8C; // Hex number var_dump($x); echo "<br>"; $x = 047; // Octal number var_dump($x); ?>


PHP Float

Floating point numbers are numbers with decimal parts or exponential form.

In the following examples we will test different numbers. PHP var_dump () The function returns the data type and value of the variable:

Examples

<?php $x = 10.365; var_dump($x); echo "<br>"; $x = 2.4e3; var_dump($x); echo "<br>"; $x = 8E-5; var_dump($x); ?>


PHP Boolean

Boolean can be TRUE or FALSE.

$ x = true;
$ y = false;

Boolean is usually used for conditional judgment. You will learn more about conditional control in the following chapters.


PHP array

Array can store multiple values in one variable.

An array was created in the following example, Then use PHP var_dump () The function returns the data type and value of the array:

Examples

<?php $cars=array("Volvo","BMW","Toyota"); var_dump($cars); ?>

You will learn more about arrays in the following chapters.


PHP Object

Object data types can also be used to store data.

In PHP, objects must be declared.

First, you must declare a class object using the class keyword. A class is a structure that can contain properties and methods.

Then we define the data type in the class and then use the data type in the instantiated class:

Examples

<?php class Car { var $color; function __construct($color="green") { $this->color = $color; } function what_color() { return $this->color; } } ?>

The PHP keyword this in the above example is a pointer to the current object instance, and does not point to any other objects or classes.

You will learn more about objects in the following chapters.


PHP NULL Value

A NULL value indicates that the variable has no value. NULL is a value whose data type is NULL.

A NULL value indicates whether a variable is null. The same can be used for the difference between data null and NULL values.

Variable data can be cleared by setting the variable value to NULL:

Example

<?php $x="Hello world!"; $x=null; var_dump($x); ?>