PHP Data types


In this article, we will learn about PHP data types and how to use them effectively in the web application.

php data types

PHP Data Types
PHP Data Types: Scalar Types
PHP Data Types: Compound Types
PHP Data Types: Special Types

The data type specifies the amount of memory that allocates to the variable associated with it. In addition, the data type determines the operations that you can perform on it.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
  2. Compound Types
  3. Special Types

 

PHP allows eight different types of data types. All of them are discussed below. The first five are called simple data types and the last three are compound data types.

PHP Data Types: Scalar Types

In simple words, a variable is called scalar type if it holds singular value only. There are 4 scalar data types in PHP.

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

PHP Data Types: Compound Types

In contrast to Scalar data types, a variable is called compound if it holds multiples values within. 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 Scalar Data Types

  • Boolean

Booleans are like a switch which has only two possible values either 1 (true) or 0 (false). Successful events will return true and unsuccessful events return false. NULL type values are also treated as false in Boolean. 

<!DOCTYPE html>

<head>

    <title>try to program-Boolean</title>

</head>

<body>

<?php

// Assign the value TRUE to a variable

$show_error = True;

var_dump($show_error);

?>

</body>

</html>               

Output:

bool(true)                               
  • 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).


<!DOCTYPE html>

<html lang="en">

<head>

    <title>PHP Integers</title>

</head>

<body>

 

<?php

$a = 123; // decimal number

var_dump($a);

echo "<br>";

 

$b = -123; // a negative number

var_dump($b);

echo "<br>";

 

$c = 0x1A; // hexadecimal number

var_dump($c);

echo "<br>";

 

$d = 0123; // octal number

var_dump($d);

?>

 

</body>

</html>       

 

Output:


int(123) 
int(-123) 
int(26) 
int(83)
  • PHP Floating Point Numbers or Doubles

Floating point numbers (also known as “floats”, “doubles”, or “real numbers”) are decimal or fractional numbers like demonstrated in the example below.

Example:


<!DOCTYPE html>

<html lang="en">

<head>

    <title>PHP Floats</title>

</head>

<body>

 

<?php

$a = 1.234;

var_dump($a);

echo "<br>";

 

$b = 10.2e3;

var_dump($b);

echo "<br>";

 

$c = 4E-10;

var_dump($c);

?>

 

</body>

</html>     

Output:


float(1.234) 
float(10200) 
float(4.0E-10)
  • String 

In a string, 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 it will be treated differently while printing variables. To clarify this look at the example below.

<!DOCTYPE html>

<html lang="en">

<head>

    <title>PHP Strings</title>

</head>

<body>

 

<?php

$a = 'Hello world!';

echo $a;

echo "<br>";

 

$b = "Hello world!";

echo $b;

echo "<br>";

 

$c = 'Stay here, I\'ll be back.';

echo $c;

?>

 

</body>

</html>                                              

Output:


Hello world!
Hello world!
Stay here, I'll be back.

PHP Compound DataTypes

  • Array

An array is a variable that can hold more than one value at a time. It is useful to aggregate a series of related items together, for example, a set of country or city names.

Example:


<!DOCTYPE html>

<html lang="en">

<head>

    <title>PHP Arrays</title>

</head>

<body>

 

<?php

$colors = array("Red", "Green", "Blue");

var_dump($colors);

echo "<br>";

 

$color_codes = array(

    "Red" => "#ff0000",

    "Green" => "#00ff00",

    "Blue" => "#0000ff"

);

var_dump($color_codes);

?>

 

</body>

</html>                                              

Output:


array(3) { [0]=> string(3) "Red" [1]=> string(5) "Green" [2]=> string(4) "Blue" } 
array(3) { ["Red"]=> string(7) "#ff0000" ["Green"]=> string(7) "#00ff00" ["Blue"]=> string(7) "#0000ff" }

 

  • Object

An object is a data type which stores not only data but also information on how to process that data. Unlike the other data types in PHP, an object must be explicitly declared.

Example:


<!DOCTYPE html>

<html lang="en">

<head>

    <title>PHP Objects</title>

</head>

<body>

 

<?php

// Class definition

class greeting{

    // properties

    public $str = "Hello World!";

   

    // methods

    function show_greeting(){

        return $this->str;

    }

}

 

// Create object from class

$message = new greeting;

var_dump($message);

?>

 

</body>

</html>                                              

Output:


object(greeting)#1 (1) { ["str"]=> string(12) "Hello World!" }

PHP Special Data Types

  •  Null

The special NULL value is used to represent empty variables in PHP. A variable of type NULL is a variable without any data. NULL is the only possible value of type null.   Example:


<!DOCTYPE html>

<html lang="en">

<head>

    <title>PHP NULL Value</title>

</head>

<body>

 

<?php

$a = NULL;

var_dump($a);

echo "<br>";

 

$b = "Hello World!";

$b = NULL;

var_dump($b);

?>

 

</body>

</html>                                              

Output:


NULL 
 NULL

 

  • Resources

A resource is a special variable, holding a reference to an external resource.

Example:


<!DOCTYPE html>

<html lang="en">

<head>

    <title>PHP Resources</title>

</head>

<body>

 

<?php

// Open a file for reading

$handle = fopen("note.txt", "r");

var_dump($handle);

?>

 

</body>

</html>                                              

Output:


resource(2) of type (stream)