Friday 17 October 2014

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

<?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

<?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

<?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

<?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

<?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

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

No comments:

Post a Comment