Control statements and data types in php

PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:

  1. Scalar Types (predefined)
  2. Compound Types (user-defined)
  3. Special Types

PHP Data Types: Scalar Types

It holds only single value. There are 4 scalar data types in PHP.

  1. boolean
  2. integer
  3. float
  4. string

PHP Data Types: Compound Types

It can hold multiple values. There are 2 compound data types in PHP.

  1. array
  2. object

PHP Data Types: Special Types

There are 2 special data types in PHP.

  1. resource
  2. NULL

PHP Boolean

Booleans are the simplest data type works like switch. It holds only two values: TRUE (1) or FALSE (0). It is often used with conditional statements. If the condition is correct, it returns TRUE otherwise FALSE.

Example:

Output:

PHP Integer

Integer means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without fractional part or decimal points.

Rules for integer:

  • An integer can be either positive or negative.
  • An integer must not contain decimal point.
  • Integer can be decimal (base 10), octal (base 8), or hexadecimal (base 16).
  • The range of an integer must be lie between 2,147,483,648 and 2,147,483,647 i.e., -2^31 to 2^31.

Example:

Output:

Decimal number: 34
Octal number: 163
HexaDecimal number: 69

PHP Float

A floating-point number is a number with a decimal point. Unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign.

Example:

Output:

Addition of floating numbers: 73.812

PHP String

A string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special characters.

String values must be enclosed either within single quotes or in double quotes. But both are treated differently. To clarify this, see the example below:

Example:

Output:

Hello Javatpoint
Hello $company

PHP Array

An array is a compound data type. It can store multiple values of same data type in a single variable.

Example:

Output:

array(3) { [0]=> string(13) "Royal Enfield" [1]=> string(6) "Yamaha" [2]=> string(3) "KTM" }
Array Element1: Royal Enfield
Array Element2: Yamaha
Array Element3: KTM

You will learn more about array in later chapters of this tutorial.

PHP object

Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared.

Example:

Output:

Bike Model: Royal Enfield

This is an advanced topic of PHP, which we will discuss later in detail.

PHP Resource

Resources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources. For example - a database call. It is an external resource.

This is an advanced topic of PHP, so we will discuss it later in detail with examples.

PHP Null

Null is a special data type that has only one value: NULL. There is a convention of writing it in capital letters as it is case sensitive.

The special type of data type NULL defined a variable with no value.

Example:

Output:


Data Types define the type of data a variable can store. PHP allows eight different types of data types. All of them are discussed below. There are pre-defined, user-defined, and special data types.

The predefined data types are:

  • Boolean
  • Integer
  • Double
  • String

The user-defined (compound) data types are:

  • Array
  • Objects

The special data types are:

  • NULL
  • resource

The first five are called simple data types and the last three are compound data types: 
 

1. Integer: Integers hold only whole numbers including positive and negative numbers, i.e., numbers without fractional part or decimal point. They can be decimal (base 10), octal (base 8), or hexadecimal (base 16). The default base is decimal (base 10). The octal integers can be declared with leading 0 and the hexadecimal can be declared with leading 0x. The range of integers must lie between -2^31 to 2^31. 

Example: 

PHP

$deci1 = 50;

$deci2 = 654;

$octal1 = 07;

$octal = 0x45;

$sum = $deci1 + $deci2;

echo $sum;

echo "\n\n";

var_dump($sum)

?>

2. Double: Can hold numbers containing fractional or decimal parts including positive and negative numbers or a number in exponential form. By default, the variables add a minimum number of decimal places. The Double data type is the same as a float as floating-point numbers or real numbers.

Example: 

PHP

$val1 = 50.85;

$val2 = 654.26;

$sum = $val1 + $val2;

echo $sum;

echo "\n\n";

var_dump($sum)

?>

Output

705.11

float(705.11)

3. String: Hold letters or any alphabets, even numbers are included. These are written within double quotes during declaration. The strings can also be written within single quotes, but they will be treated differently while printing variables. To clarify this look at the example below. 
 

Example: 

PHP

$name = "Krishna";

echo "The name of the Geek is $name \n";

echo 'The name of the geek is $name ';

echo "\n\n";

var_dump($name)

?>

Output

The name of the Geek is Krishna 
The name of the geek is $name 

string(7) "Krishna"

4. Boolean: Boolean data types are used in conditional testing. Hold only two values, either TRUE(1) or FALSE(0). Successful events will return true and unsuccessful events return false. NULL type values are also treated as false in Boolean. Apart from NULL, 0 is also considered false in boolean. If a string is empty then it is also considered false in boolean data type. 

Example:   

PHP

if(TRUE)

    echo "This condition is TRUE";

if(FALSE)

    echo "This condition is not TRUE";

?>

Output

This condition is TRUE

5. Array: Array is a compound data type that can store multiple values of the same data type. Below is an example of an array of integers. It combines a series of data that are related together.
  

PHP

$intArray = array( 10, 20 , 30);

echo "First Element: $intArray[0]\n";

echo "Second Element: $intArray[1]\n";

echo "Third Element: $intArray[2]\n\n";

var_dump($intArray);

?>

Output

First Element: 10
Second Element: 20
Third Element: 30

array(3) {
  [0]=>
  int(10)
  [1]=>
  int(20)
  [2]=>
  int(30)
}

We will discuss arrays in detail in further articles. 
 

6. Objects: Objects are defined as instances of user-defined classes that can hold both values and functions and information for data processing specific to the class. This is an advanced topic and will be discussed in detail in further articles. When the objects are created, they inherit all the properties and behaviours from the class, having different values for all the properties.

          Objects are explicitly declared and created from the new keyword.

PHP

class gfg {

  var $message;

  function gfg($message) {

    $this->message = $message;

  }

  function msg() {

    return "This is an example of " . $this->message . "!";

  }

}

$newObj = new gfg("Object Data Type");

echo $newObj -> msg();

?>

Output

This is an example of Object Data Type!

7. NULL: These are special types of variables that can hold only one value i.e., NULL. We follow the convention of writing it in capital form, but it’s case-sensitive. If a variable is created without a value or no value, it is automatically assigned a value of NULL. It is written in capital letters.

Example: 

PHP

$nm = NULL;

echo $nm;   

var_dump($nm);

?>

8. Resources: Resources in PHP are not an exact data type. These are basically used to store references to some function call or to external PHP resources. For example, consider a database call. This is an external resource. Resource variables hold special handles for files and database connections.
We will discuss resources in detail in further articles.

Note: 

  • To check the type and value of an expression, use the var_dump() function which dumps information about a variable. 
  • PHP allows the developer to cast the data type.

This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
 


What is object data type in PHP?

In PHP, Object is a compound data type (along with arrays). Values of more than one types can be stored together in a single variable. Object is an instance of either a built-in or user defined class. In addition to properties, class defines functionality associated with data.

What is the default data type in PHP?

PHP's variables are dynamic, and change depending on the data inside them. So they have no datatype by default.

What are the four scalar types of PHP?

There are 4 scalar data types in PHP..
boolean..
integer..
float..
string..

What are the different data types?

data type.