3 Data Types in PHP with Examples

In this article we are going to cover Data Types in PHP with Examples.

What is Data Types in PHP?

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

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

The scalar (predefined) data types are:

  • String
  • Integer
  • Float
  • Boolean

The user-defined (compound) data types are:

  • Object
  • Array

The special data types are:

  • NULL
  • Resource

#1. String in PHP with Examples:

String is a sequence of characters.

String can be any text inside quotes. You can use single or double quotes. Strings can hold letters or any alphabets, even numbers are included.

Example of String in PHP:

<!DOCTYPE html>
<html>
<body>

<?php 
$a1 = "Hello I am Shweta";
$a2 = 'Hello I am Shweta';

echo $a1;
echo "<br>"; 
echo $a2;
?>

</body>
</html>

Output:

Hello I am Shweta
Hello I am Shweta
3 Data Types in PHP with Examples 1

Another Example of String in PHP:

<!DOCTYPE html>
<html>
<body>

<?php 
$name = "Shweta";
echo 'Hello I am $name';
    echo "<br>"; 
echo "Hello I am $name";
  
?>

</body>
</html>

Output:

Hello I am $name
Hello I am Shweta
3 Data Types in PHP with Examples 2

#2. Integer in PHP with Examples:

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

An integer can be either positive or negative.

An integer must have at least one digit.

An integer must not have a decimal point.

Integer can be decimal (base 10), octal (base 8), or hexadecimal (base 16).

Example of Integer in PHP:

(Octal, hexadecimal numbers convert into decimal)

<!DOCTYPE html>
<html>
<body>

<?php   
    $dec1 = 74;  
    $oct1 = 043;  
    $hexa1 = 0x55;  
    echo "Decimal number: " .$dec1. "</br>";  
    echo "Octal number: " .$oct1. "</br>";  
    echo "HexaDecimal number: " .$hexa1. "</br>";  
?>  

</body>
</html>

Output:

Decimal number: 74
Octal number: 35
HexaDecimal number: 85
3 Data Types in PHP with Examples 3

Another example of Integer in PHP:

<!DOCTYPE html>
<html>
<body>

<?php  
$x = 0x45;
var_dump($x);
?>  
</body>
</html>

Output:

int(69)
3 Data Types in PHP with Examples 4

#3. Float in PHP with Examples:

A float is a number with decimal point. It can hold numbers with a fractional or decimal point, including a negative or positive numbers.

Example of Float in PHP:

<!DOCTYPE html>
<html>
<body>
<?php   
    $num1 = 19.34;  
    $num2 = 34.472;  
    $sum = $num1 + $num2;  
    echo "Addition of floating numbers: " .$sum;  
?>
</body>
</html>

Output:

Addition of floating numbers: 53.812
3 Data Types in PHP with Examples 5

Another Example of Float in PHP:

<!DOCTYPE html>
<html>
<body>
<?php  
$x = 99.999;
var_dump($x);
?> 
</body>
</html>

Output:

float(99.999)
3 Data Types in PHP with Examples 6

#4. Boolean in PHP with Examples:

Booleans are the simplest data type it works like switch. Boolean data types are used in conditional testing. It holds any two values either TRUE(1) or FALSE(0). If the condition is correct, it returns TRUE otherwise FALSE.

NULL type values are also treated as false in Boolean. In Boolean a string is empty then it is considered false Boolean data type.

Example of Boolean in PHP:

<!DOCTYPE html>
<html>
<body>
<?php   
    if (TRUE)  
        echo "This condition is TRUE.";  
    if (FALSE)  
        echo "This condition is FALSE.";  
?>  
</body>
</html>

Output:

This condition is TRUE.
3 Data Types in PHP with Examples 7

#5. Objects in PHP with Examples:

Objects are instances of user-defined classes that can store both value and functions.

They inherit all the properties and behavior from the class, having different values for all the properties. They must be explicitly declared.

Example of Object in PHP:

<!DOCTYPE html>
<html>
<body>
<?php   
     class car {  
          function model() {  
               $model_name = "Duster";  
               echo "Car Model: " .$model_name;  
             }  
     }  
     $obj = new car();  
     $obj -> model();  
?>    
</body>
</html>

Output:

Car Model: Duster
3 Data Types in PHP with Examples 8

#6. Array in PHP with Examples:

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

Example of Array in PHP:

<!DOCTYPE html>
<html>
<body>
<?php   
    $cars = array ("Duster", "Toyota Fortuner", "Mahindra Scorpio");  
    var_dump($cars);   //the var_dump() function returns the datatype and values  
    echo "</br>";  
    echo "Array Element1: $cars[0] </br>";  
    echo "Array Element2: $cars[1] </br>";  
    echo "Array Element3: $cars[2] </br>";  
?>   
</body>
</html>

Output:

array(3) { [0]=> string(6) "Duster" [1]=> string(15) "Toyota Fortuner" [2]=> string(16) "Mahindra Scorpio" }
Array Element1: Duster
Array Element2: Toyota Fortuner
Array Element3: Mahindra Scorpio
3 Data Types in PHP with Examples 9

#7. Null Value in PHP with Examples:

Null is a special data types in PHP that has only one value: NULL. A variable of data type NULL is a variable that has no value assigned. It is case sensitive.

Example of NULL Value in PHP:

<!DOCTYPE html>
<html>
<body>
<?php
$x = null;
var_dump($x);
?>
</body>
</html>

Output:

NULL
3 Data Types in PHP with Examples 10

#8. Resource in PHP with Examples:

Resources are not data types in PHP. It is used to store of a reference to function and resources external to PHP.

For Example:

A database call. It is external resource.

This is an advances topic of PHP, so we will discuss it later in detail with examples, we have covered Data Types in PHP.

Conclusion:

In this article we have covers Data Types in PHP with Examples.

Related Articles:

Reference:

PHP Data Types official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap