PHP

PHP Data Types

PHP supports the following basic data types:

  • Integer—Used for whole numbers
  • Float (also called double)—Used for real numbers
  • String—Used for strings of characters
  • Boolean—Used for true or false values
  • Array—Used to store multiple data items
  • Object—Used for storing instances of classes
Two special types are also available:
  • NULL
  • Resource

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 = 5786;      //positive number
    var_dump($x);
    echo "<br />";
    $x = -321;      //negative number
    var_dump($x);
    echo "<br />";
    $x = 0xF;       //hexadecimal number
    var_dump($x);
    echo "<br />";
    $x = 011;       //octal number
    var_dump($x);
?>

Output:

Tutorialink.com
int(5786) 
int(-321) 
int(15) 
int(9)

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 = 24.365;
    var_dump($x);
    echo "<br />";
    $x = 1.5e3;
    var_dump($x);
?>

Output:

Tutorialink.com
float(24.365) 
float(1500)

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
    $name = "Hello world!"; //double quote 
    echo $name;
    echo "<br />";
    $name = 'Hello world!'; //single quote
    echo $name;
?>

Output:

Tutorialink.com
Hello world!
Hello world!

PHP Booleans

Booleans can be either TRUE or FALSE.
Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.

Example:

<?php
    $x = true;
    $y = false;
?>

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:
You will learn a lot more about arrays in later chapters of this tutorial.

Example:

<?php
    $language = array("C","C++","JAVA");
    var_dump($language);
?>

Output:

Tutorialink.com
array(3) { [0]=> string(1) "C" [1]=> string(3) "C++" [2]=> string(4) "JAVA" }

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:
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
    $var1 = "";    //empty string
    var_dump($var1);
    echo "<br />";
    $var2 = null;  //null value
    var_dump($var2);
?>

Output:

Tutorialink.com
string(0) "" 
NULL

PHP Resorce Value

Certain built-in functions (such as database functions) return variables that have the type resource. They represent external Resources Value(such as database connections).
You will almost certainly not directly manipulate a resource variable, but frequently they are returned by functions and must be passed as parameters to other functions.

Example:

<?php
    $con = mysqli_connect(host,username,password,dbname);
    /*here mysqli_connect() function returns Resource Value(database connection)*/
?>

In the above example we store the connection in a variable ($con)




Subscribe us on Youtube

Share This Page on


Ask Question