PHP Data Types

PHP data types are used to store different types of data or values like..simple string and a numeric integer. PHP supports several primitive data types that can be categorized below:

  • Integer
  • Boolean
  • String
  • Float
  • Array
  • Object
  • NULL
  • Resource


PHP Integers

Integers are whole numbers including positive and negative numbers and without a decimal point (…, -2, -1, 0, 1, 2, …).Integers can be decimal (base 10), octal (base 8 – prefixed with 0x) or hexadecimal (base 16 – prefixed with 0x).

Example

<?php 
  
// decimal base integers 
$dec1 = 50;  
$dec2 = 650;  
  
// octal base integers 
$octal = 057;  
  
// hexadecimal base integers 
$hexa = 0x28;  
  
$sum = $dec1 + $dec2; 
echo $sum; 
  
?> 

Output:

700

In the given below example $x is an integer. The PHP var_dump() function returns the data type and value:

Example

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

Output:

int(234)




PHP Strings

A string is a sequence of characters that’s including letters, numerals, symbols, punctuation marks, etc.The strings can be written within single quotes (e.g. ‘Welcome to Netzole!’) and can also be written within double quotes (“Welcome to Netzole!”).

Example

<?php
$str1 = "Hello";
$str2 = 'Welcome To Netzole';
$str3 = "1001";

echo $str1;
echo "<br/>";
echo $str2;
echo "<br/>";
echo " My Office No. is $str3";

Output:

Hello
Welcome To Netzole
My Office No. is 1001




PHP Float

Float data type is used to hold any decimal numeric value or a number in exponential form, it can be store either negative or positive values. And this is also known as “doubles”, or “real numbers”.

Example

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

Output:

float(45.365)




PHP Boolean

A boolean data type can represent two possible values, either True or False.

Example

$x = true;
$y = false;

NOTE: In above given example values true and false are not enclosed within quotes, because these are not strings.


PHP Arrays

An array is a variable that can store multiple values in one single variable. There are two method to declare an array which is given below:

  • Using the array() function.
  • Wrapping with [ and ].

Example

<?php
$array = array('Apple', 'Kiwi', 'Orange', 'Mango');
var_dump($array);

$array = ['Apple', 'Kiwi', 'Orange', 'Mango'];
var_dump($array);

Output:

array(4) { [0]=> string(5) “Apple” [1]=> string(6) “Kiwi” [2]=> string(6) “Orange” [3]=> string(5) “Mango” }
array(4) { [0]=> string(5) “Apple” [1]=> string(6) “Kiwi” [2]=> string(6) “Orange” [3]=> string(5) “Mango” }



PHP Objects

An object is a particular instance of a class where it can be a combination of variables, functions, and data structures.

Example

<?php
class Car {
    function Car() {
        $this->name= "Audi";
    }
}

// create an object
$obj= new Car();

// show object properties
echo $obj->name;
?>

Output:

Audi




PHP NULL

This data type 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.Null means no value. And Null is not 0, false or any other value, it is null.

Example

<?php
$y = "Hello!";
$y = null;
var_dump($y);
?>

Output:

NULL




PHP Resources

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

Pin It on Pinterest

Shares
Share This