Home / Core PHP / PHP 5 Data Types

PHP 5 Data Types

String, Integer, Floating point numbers, Boolean, Array, Object, NULL.
PHP Strings
A string is a sequence of characters, like “Hello world!”.

A string can be any text inside quotes. You can use single or double quotes:

Example

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

PHP Integers
An integer is a number without decimals.

Rules for integers:

An integer must have at least one digit (0-9)
An integer cannot contain comma or blanks
An integer must not have a decimal point
An integer can be either positive or negative
Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based – prefixed with 0x) or octal (8-based – prefixed with 0)
In the following example we will test different numbers. The PHP var_dump() function returns the data type and value of variables:

Example

Select Code
<?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
A floating point number is a number with a decimal point or a number in exponential form.

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

Example

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


PHP Booleans
Booleans can be either TRUE or FALSE.

$x=true;
$y=false;
Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.

PHP Arrays
An array stores multiple values in one single variable.

In the following example we create an array, and then use the PHP var_dump() function to return the data type and value of the array:

Example

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

You will learn a lot more about arrays in later chapters of this tutorial.

PHP Objects
An object is a data type which stores data and information on how to process that data.

In PHP, an object must be explicitly declared.

First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods.

We then define the data type in the object class, and then we use the data type in instances of that class:

Example

Select Code
<?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 a later chapter of this tutorial.

PHP NULL Value
The special NULL value represents that a variable has no value. NULL is the only possible value of data type NULL.

The NULL value identifies whether a variable is empty or not. Also useful to differentiate between the empty string and null values of databases.

Variables can be emptied by setting the value to NULL:

Example

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

About v.shakya

I am V.Shakya, Software Developer & Consultant I like to share my ideas, views and knowledge to all of you who come across my website. I am young, enthusiastic, highly motivated and self disciplined person. I completed my studies in Master of Computer Application and currently giving my technical expertise to one of the Big IT company. I have more than fifteen years of experience in vast field of Programming , Designing and Development of websites and various software's.

Check Also

PHP 5 Arrays

An array stores multiple values in one single variable: Example <?php $cars=array("Volvo","BMW","Toyota"); echo "I like …

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.