PHP Tutorial


PHP 7 Data Types

Author:- w3alert.com
Updated Date:-01-02-2020

Data Types in PHP

PHP provides several data types that can be stored data in variables. PHP has allowed eight types of data types.

there are five simple data types in PHP like String, Integer, Float, Boolean, null and the remaining three are compound data types like Array, Object, Resource.

NOTE:- data types are used to store different types of data in PHP.

The following data types in PHP:

  • String
  • Integer
  • Float (floating point/ Decimal/ Double)
  • Boolean
  • NULL
  • Array
  • Object
  • Resource

PHP String

In PHP, string can be used to store characters, alphabet, numbers. Or A string is a group of special characters (#,*,&,%,...), letters (a,b,c,d,...) and numbers (1,2,3,4,...) like: "Hello w3alert!".

NOTE:- String can be lengthy up to 2GB (2147483647 bytes max.).  

The following example to understand String Data Type:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Strings Examples</title>
</head>
<body>

<?php
$num1 = "Hello w3alert!";
echo $num1;
echo "<br>";

$num2 = "I am a developer.";
echo $num2;
echo "<br>";
?>

</body>
</html>

PHP Integer

Integers can have both zero, positive (1,2,3,...) and negative (-1,-2,-3,...) values but no decimal values (1.2,3.4,4.50,...). 
Integers can be specified in decimal (base 10), hexadecimal (base 16 - prefixed with 0x) or octal (base 8 - prefixed with 0) notation, optionally preceded by a sign (- or +).

The following example to understand Integer Data Type:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Integer Examples</title>
</head>
<body>

<?php
$num1 = 127; // decimal number
var_dump($num1);
echo "<br>";

$num2 = -150; // a negative number
var_dump($num2);
echo "<br>";

$num3 = F5A; // hexadecimal number
var_dump($num3);
echo "<br>";

$num4 = 567; // octal number
var_dump($num4);
?>
</body>
</html>

 

PHP Floating Point Numbers or Double

In PHP,Float is used to hold decimal/float/double/real numbers.
Floating point numbers (Floats, Doubles, Real Numbers) are decimal numbers like: 3.4, 1.47, 5.8,... .

The following example to understand Float Data Type:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Floats Examples</title>
</head>
<body>

<?php
$num1 = 1.564;
var_dump($num1);
echo "<br>";

$num2 = 67.23;
var_dump($num2);
echo "<br>";

$num3 = 5.8;
var_dump($num3);
?>

</body>
</html>

PHP Boolean

A Boolean data type works with two possible conditions such as 1 (true) or 0 (false).
If a program is error free, it returns the true value 1 and if the program has some objections, it returns the false value 0.

The following example to understand Booleans Data Type:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Booleans Examples</title>
</head>
<body>

<?php  

// Assign the value TRUE to a variable
$num1 = true;
if($num1 == TRUE) 
    echo "Return value TRUE";
else 
    echo "Return value False"; 
echo "<br>";

// Assign the value False to a variable
$num2 = true;
if($num2 == False)
    echo "Return value True"; 
else 
    echo "Return value False"; 
?> 

</body>
</html>

PHP Array

The php array holds one or more than one value in a PHP variable. There are 3 types of array in PHP like: Indexed Array, Associative Array, Multidimensional Array.

you can define array from two types:

1.  [];        //it is a first way.
2.  array();   //it is a second way.

The following example to understand Array Data Type:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Arrays</title>
</head>
<body>

<?php
$fruits = array("mango", "apple", "orange");
var_dump($fruits);
echo "<br>";

$Vegetable = ["carrots", "beans", "leeks"];
var_dump($fruits);
?>

</body>
</html>

PHP Object

An object data type holds data and Information on how to process that data. Objects are different from other PHP data types. Because they hold not only the data itself but also all the information on how that particular data can be processed.

The following example to understand Object Data Type:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Object Examples</title>
</head>
<body>

<?php
// Class definition
class welcome{
// properties
public $str = "Hello w3alert!";

// methods
function show_welcome(){
return $this->str;
}
}

// Create object from class
$message = new welcome;
var_dump($message);
?>

</body>
</html>

PHP NULL

Data type null in php, which has no value in the variable or This is a special type of data type that holds the null value in the PHP variable.

For e.g. When a variable is defined in php but does not assign any value to it, it will be automatically stored NULL in it.

The following example to understand NULL Data Type:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP NULL Value Example</title>
</head>
<body>

<?php
$x = NULL;
var_dump($x);
echo "<br>";

$y = "Hello w3alert!";
$y = NULL;
var_dump($y);
?>

</body>
</html>

PHP Resource

The php resource datatype is not the original data type. Because it is used to store references to other functions and external resources.

The following Example to understanding PHP Resource Data Type:

<?php
    $host='localhost';
    $username='root';
    $password='';
    $dbname = "test";
    $connection = mysqli_connect($host,$username,$password,"$dbname");
    if(!$connection)
    {
    die('Could not Connect MySql Server:' .mysql_error());
    }
?>
The above function will return a resource type data to be stored into $connection variable.