PHP Data Types

What Is Data Types?

The data type is used to specify which kind of data the variable can hold. It is generally used to determine which type of data variable can hold. However, PHP is a loosely typed language so there is no need to specify the datatype while declaring the variables.

 PHP supports 8 primitive data types that can be categorized further into 3 types.

Scaler Types?

The scaler type holds only a single value. There are four scalar data types in PHP.

  1. Boolean
  2. Integer
  3. Float
  4. String

Boolean:

It represents two possible states: TRUE or FALSE.

<?php
$sts = true;
$loginSts =false;
?>

Integer:

Integer means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without fractional parts or decimal points.  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 +).

<?php   
    $dec1 = 34;  
    $oct1 = 0243;  
    $hexa1 = 0x45;  
    echo "Decimal number: " .$dec1. "</br>";  
    echo "Octal number: " .$oct1. "</br>";  
    echo "HexaDecimal number: " .$hexa1. "</br>";  
?>

Float:

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

<?php
$x = 20.23;
var_dump($x);
?>

String:

Strings are sequences of characters, like “Hello World”. In PHP we can declare a string either within a single quote or a double quote.

<?php
$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "<br>";
echo $y;
?>

Compound Types?

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

  1. Array
  2. Object

Array:

An array is a variable that can hold more than one value at a time.

<?php
$colors = array("Red", "Green", "Blue");
var_dump($colors); 
?>

Object:

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

<?php   
     class language {  
          function getName() {  
               $langName = "PHP";  
               echo "Language Name: " .$langName;  
             }  
     }  
     $obj = new language();  
     $obj -> getName();  
?>

Special Types?

There are two special data types in PHP.

  1. Null.
  2. Resource

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.

<?php 
$x = null;
 var_dump($x)
?>

Resource:

A resource is a special variable, that holds a reference to an external resource. Resource variables typically hold special handlers for opened files and database connections.

<?php
// Connect to MySQL database server with default setting
$link = mysqli_connect("localhost", "root", "");
var_dump($link);
?>

 

 

Previous                                                                                                                    Next

Back to top button