Data Types in PHP

Data types in PHP are used to hold different types of data or values. PHP supports the following data types :
  • String
  • Integer
  • Float ( also called double )
  • Boolean
  • Array
  • Object
  • NULL

String data type : A string is a sequence of characters, like "Welcome to PHP". A string can be any text inside quotes. You can use single or double quotes.

Example :
<?php
$name = "Welcome to Neoogy";
echo $name;
?>

Output :
Welcome to Neoogy


Integer data type : An integer data type are whole numbers, without a decimal point, like 123.

Example :
<?php
$x = 10;
echo $x."<br>";
echo var_dump($x); // var_dump() function returns the data type and value
?>

Output :
10
int(10)


Float data type : A float (floating point number) data type are numbers with a decimal point.

Example  :
<?php
$y = 3.14; // $y is a float
echo $y;
?>

Output :
3.14


Boolean data type : A boolean data type have only two possible values either true or false. Booleans are often used in conditional statements.


Array data type : An array stores multiple values in one single variable.

Example :
<?php
$name = array("Alokesh","Ashim","Bickey","Mridul");
var_dump($name);
?>

Output :
array(4) { [0]=> string(7) "Alokesh" [1]=> string(5) "Ashim" [2]=> string(6) "Bickey" [3]=> string(6) "Mridul" }


Object data type : An object is a data type which stores data and information on how to process that data.

Example :
<?php
class myCar {
    function myCar() {
        $this->model = "Audi";
    }
}
// create an object
$herbie = new myCar();
// show object properties
echo $herbie->model;
?>

Output :
Audi


NULL data type : Null is a special data type which can have only one value: NULL.

Example :
<?php
$x = NULL;
echo $x;
?>

Output :


0 Comments