PHP Tutorial: Data Types

Data Types

Datatype refers to the type of data stored in variables. There are 8 types of datatype in PHP.

  1. Integer
  2. Float (floating point numbers - also called double)
  3. String
  4. Boolean
  5. Array
  6. Object
  7. NULL
  8. Resource

There is no need to define a type of the variable in PHP, because PHP is a loosely-typed language. PHP variable can easily move between types as required by the code.

 

Integer

An integer is a whole number (without decimal). It can be a number between -2,147,483,648 and +2,147,483,647.

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)

Valid decimal integers:

1

123

+7

-1007395

An octal number is a base-8 number. Each digit can have a value between zero (0) and seven (7). Octal numbers are commonly used in computing because a three digit binary number can be represented as a one digit octal number. Octal numbers are preceded by a leading zero (e.g., 0123).

Valid octal integers:

01

0123

+07

-01007345

A hexadecimal number is a base-16 number. Each digit can have a value between zero (0) and F. Since we only have ten numbers in our numbering system (0-9), we use the letters A through F to make up the difference for hexadecimal values.

Valid hexadecimal integers:

0x1

0xff

0x1a3

+0x7

-0x1ab7345

 

Float

A float (floating point number) is a number with a decimal point or a number in exponential form.

In the following example $x is a float. The PHP var_dump() function returns the data type and value.

Floating-point numbers are also known as real numbers. They are numbers that have a fractional component or a decimal point. Floating-point numbers get their name from their decimal point.

PHP recognizes two types of floating point numbers. The first is a simple numeric literal with a decimal point. The second is a floating-point number written in scientific notation. Scientific notation takes the form of [number]E[exponent], for example, 1.75E-2.

Example

3.14

0.001

-1.234

0.314E2   //  31.4

1.234E-5  //  0.00001234

-3.45E-3  // -0.00345

 

String

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

A string can be enclosed in quotes, either double or single quotes.

Strings inside double quotes are parsed, or interpolated, while strings inside single quotes aren't. That means we can include variables or special characters in double-quoted strings, those values are processed and become part of the string. If we putting variables and special characters in single-quote then the PHP will be output the exact variable and special characters that we typed inside single quote. In other words, they are literals. This means that you can embed variables directly inside strings in PHP when using double quotes, but not when using single quotes. 4

Example

$myVar = "xyz";       // assign a string value to $myVar

echo $myVar;          // output 'xyz'

echo "abc to $myVar"; // output 'abc to xyz'

// but with single quotes

echo 'abc to $myVar': // output 'abc to $myVar'

Special Characters

Anything inside the quotes is part of a string, even numbers. Thus, "123" is a string, not a number. But there are special characters which cannot be included directly in a quote.

For instance, we can't have double quotes inside a double-quoted string, also we can't have single quotes inside a single-quote string. To include these special characters, we need to escape them so that the parser knows that they are literal characters and not string delimiters.

In PHP, we can escape a character by preceding it with a backslash (\).

There are two types of escaped characters in PHP.

There are those that are a special character in the language and need to be escaped to get the parser to treat them as a literal.

For example, \" means that the quote after the backslash is a literal quote, and \\ is used to include a literal backslash in a string. There are also those that are normal characters that serve as special characters when they are escaped.

For example, \n inserts a line break in the string at that location, and \r inserts a carriage return.

 

Boolean

A Boolean represents two possible states: TRUE or FALSE.

Booleans only have two values, true and false. Booleans are often used in conditional testing. TRUE means 1 and FALSE means 0.

 

Array

An array is a variable that can hold a group of values. Arrays are usually meant to store a collection of related data elements, although this is not a necessity. You access the individual elements by referring to their index position within the array. The position is either specified numerically or by name.

  • An array with a numeric index is commonly called an indexed array.

         $listing[0] = "first item";

         $listing[1] = "second item";

         $listing[2] = "third item";

  • An array with named positions is called an associated array.

          $arName["item1"] = "abc";

         $arName["item2"] = "def";

         $arName["item3"] = "ghi";

In PHP, all arrays are associative, but you can still use a numeric index to access them. Indexed arrays start at position zero that means first element will be at positon 0.

Example

<?php

$cars = array("Volvo","BMW","Toyota");

var_dump($cars);

?>

In above example $cars is an array. The PHP var_dump() function returns the data type and value.

 

Object

PHP also can handle objects like any other object oriented programming languages.

An object is a data type that allows for the storage of not only data but also information on how to process that data.

The data elements stored within an object are referred to as its properties, also sometimes called the attributes of the object. The information, or code, describing how to process the data compromises what are called the methods of the object.

Objects have two components. First, you must declare a class of object. It defines the structure of the object to be constructed. Then you instantiate the object, which means you declare a variable to be of a certain class and assign values to it appropriately.

Another way of looking at objects is that they allow you to create your own data types. You define the data type in the object class, and then you use the data type in instances of that class.

Yes, there is also an is_object() function to test whether a variable is on object instance.

Example

<?php
class Car {
    function Car() {
        $this->model = "VW";
    }
}

// create an object
$herbie = new Car();

// show object properties
echo $herbie->model;
?>

 

NULL

Null is a special data type which can have only one value, which is itself (NULL). Means a variable of data type NULL is a variable that has no value assigned to it.

Note: If a variable is created without a value, it is automatically assigned a value of NULL.

We can say that null is not only a data type, but also a keyword.

Example

<?php

$x = "Hello world!";

$x = null;

var_dump($x);

?>

 

Resource

Resources are not an actual data type, but the storing of a reference to functions and resources external to PHP. The most common example of using the resource data type is a database call. 

Next Topic