Welcome to PHPBash. In this PHP Tutorial, we will learn about Basics of PHP Data Types. In our Previous Tutorial, we learn about different concepts related to PHP variables. Data types define what kind of data or values a variable will hold. With help of data types, computer can understand the type of data stored in variable and as per its type, memory will be allocated. so data types are very important and crucial concept in any programming language.
In PHP 7, there are total 10 different data types. They are divided in to Scalar Types, Compound Types and Special Types. As PHP is loosely typed, there is no need to specify data type while declaring variables. PHP Handles typecasting or PHP Juggling automatically. You can read more about it, in this tutorial.
1. PHP 7 Data Types and their Sub Types
Now let's start this tutorial with understanding PHP 7 Data Types and their Sub Types.
PHP 7 Data Types and their Sub Types
When variable is created in PHP, the value of that variable determines the data type of variable. Variables can store different kinds of data like numbers, strings, boolean values etc. PHP data types are used to define different types of data or values that will be stored in variables.
IN PHP 7, there are total 8 different Data Types and they are further divided in 3 different types. They are as follows:
A. Scalar Data Types
1. Integer
2. Float (floating point numbers - also called double)
3. String
4. Boolean
B. Compound Data Types
1. Array
2. Object
3. callable
4. iterable
C. Special Data Types
1. NULL
2. Resource
Now let's discuss each data type in detail.
A. Scalar Data Types
1. Integer
An Integer is number without fractional value. An integer can have positive or negative sign. It is whole number in range {..., -2, -1, 0, 1, 2, ...} without fractional or decimal point value.
In PHP, Integers can be specified in Binary (with base 2),Hexadecimal (with base 16), Decimal ( with base 10) or Octal (with base 8). As well, for defining Negative integer,the negation operator can be used. The range of Integer Data type is 2,147,483,648 and 2,147,483,647 (-2^31 to 2^31).
PHP 7 Integer Data Type- Example1:
<?php
$a = 100;
$b = -96;
echo "a=".$a;
echo "<br>";
echo "b=".$b;
?>
PHP 7 Integer Data Type- Example2:
<?php
$no1 = 0x1F; // hexadecimal number
var_dump($no1);
echo "<br>";
$no2 = -34; // a negative number
var_dump($no2);
echo "<br>";
$no3 = 22; // decimal number
var_dump($no3);
echo "<br>";
$no4 = 0b11111111; // Binary Number
var_dump($no4);
echo "<br>";
$no5 = 0156; // octal number
var_dump($no5);
echo "<br>";
?>
The above code will output:
int(31)
int(-34)
int(22)
int(255)
int(110)
2. Float or Double
PHP Float Data type stores numbers with decimal or fractional value. PHP Float Data type can store either negative or positive floating point value. PHP Float is also called as "floats", "doubles", or "real numbers".
PHP 7 Float Data Type- Example1:
<?php
$no1 = 4.897;
var_dump($no1);
echo "<br>";
$no2 = 63.2e7;
var_dump($no2);
echo "<br>";
$no3 = 9E-5;
var_dump($no3);
?>
The above code will output:
float(4.897)
float(632000000)
float(9.0E-5)
PHP 7 Float Data Type- Example2:
<?php
$no1 = 5.3;
$no2 = 8.3;
$result = $no1 + $no2;
echo "Addition : " .$result;
?>
The above code will output:
Addition : 13.6
3. String
Basically, A string is a sequence of characters enclosed in single quotes ('') or double quotes(" "). In string, each character has 1 Byte size.
The PHP String data type can store numbers, Alphabets, special characters up to 2GB (2147483647 bytes maximum) size.
PHP String can be specified in 3 different ways:
1. Single quoted
2. Double quoted
3. PHP Heredoc Syntax
4. PHP Nowdoc Syntax
1. Single quoted-
The simplest way to specify a string is to enclose it in single quotes (the character ').
PHP 7 String Data Type Example- Single quoted String:
<?php
$str1 = 'String 1';
$str2 = 'String 2';
echo "String 1".$str1;
echo "<br>";
echo "String 2".$str2;
?>
2. Double quoted -
If the string is enclosed in double-quotes ("), PHP will be able to interpret escape sequences for special characters.
PHP 7 String Data Type Example- Double quoted String
<?php
$str1 = "String 1";
$str2 = "String 2";
echo "String 1 : $str1";
echo "<br>";
echo "String 2 : $str2";
?>
3. PHP Heredoc Syntax
The PHP Heredoc and Nowdoc gives an alternative way of creating strings in PHP. When there is need to create String with more than one line or multiple lines, then PHP Heredoc and PHP Nowdoc is useful.
PHP Heredoc required to define an identifier that will mark the starting and ending of the string. The identifier may contain alphanumeric characters following the same rules like variable names declaration.
In PHP Heredoc, The closing identifier must start in the first column of the line. As well it should have same name as start.
<?php
echo <<<EXP_1
This is Multiline
String printed using PHP Heredoc Syntax.
EXP_1;
?>
The Output will be:
This is Multiline
String printed using PHP Heredoc Syntax.
IMPORTANT:
1. The opening identifier must always be preceded by the <<< operator.
2. Heredoc’s are equivalent to a double quoted string. It means, any variable string will get expanded with their values. For Example:
<?php
$ctr=10;
echo <<<EXP_1
This is Multiline
String printed using PHP Heredoc Syntax with $ctr value.
EXP_1;
?>
The Output will be:
This is Multiline
String printed using PHP Heredoc Syntax with 10 value.
The placement of the closing identifier is important. Prior to PHP 7.3 it had to always be placed on a new line followed by a semi-colon and with no white-space in front of it. So all the examples above would be valid.
4. PHP Nowdoc Syntax (with PHP 5.3.0)
PHP Nowdoc Syntax is same as PHP Heredoc Syntax Except, PHP Nowdocs are to single-quoted strings where as PHP Heredocs are to double-quoted strings. String can be specified with PHP Nowdoc same as PHP Heredoc but, there will be no parsing for variables.
IMPORTANT: To denote a PHP Nowdoc use single quotes around the opening identifier.
Example PHP Nowdoc:
<?php
echo <<<'EXP_1'
This is Multiline
String printed using PHP Nowdoc Syntax.
EXP_1;
?>
The Output will be:
This is Multiline
String printed using PHP Nowdoc Syntax.
PHP Boolean
PHP Boolean data type represent only two values that are TRUE or FALSE. The TRUE value can be represented by 1 and FALSE can be represented by 0.
The Boolean data type used in conditional and looping statements mostly for conditional testing. For Implementing Flag concept which is ON or OFF, this data type is useful. If the Condition is satisfied or correct, it returns TRUE otherwise FALSE.
Example 1:
<?php
if (10>5)
{
echo "Condition is TRUE.";
}
else
{
echo "Condition is FALSE.";
}
?>
The Output will be:
Condition is TRUE.
B. Compound Data Types
1. PHP Array
Basically Arrays are used for storing more than one values in to single variable at a time. Generally, the items with same data type can be collectively stored in Array. For example, Roll Numbers of Students can be stored in Array name roll_Numbers.
An array in PHP is actually an ordered map. A map is a type that associates values to keys. In PHP Array, All values are stored with certain Index value. Index is also known as Key which used to identify array item uniquely. Consider following example, where $numbers is an array.
Example 1: PHP array with Key
<?php
$numbers = array("one","two","three");
var_dump($numbers);
?>
The Output will be
array(3) { [0]=> string(3) "one" [1]=> string(3) "two" [2]=> string(5) "three" }
Example 2: PHP array with Key
<?php
$array = array(
1 => "a",
2 => "b",
3 => "c",
4 => "d",
);
var_dump($array);
?>
The Output will be:
array(4) { [1]=> string(1) "a" [2]=> string(1) "b" [3]=> string(1) "c" [4]=> string(1) "d" }
2. PHP Object
An object is a an instance of a class and can be act as blueprint of objects. In PHP, we need to explicitly declare an object. In order to create object, First we need to create a class using PHP class Keyword. The PHP class will consist, Methods and Properties.
Example of PHP Class and Object
<?php
class First_name
{
public $first_name = "Alex";
}
// create an object
$object1 = new First_name();
// show object properties
echo "First Name:".$object1->first_name;
?>
Keyword In above Example, We have created class First_name and has one public property with name $first_name. After defining class, we created instance of it, called Object with name $object 1 using statement-
$object1 = new First_name();
Using $object1, We are trying to access the property of class i.e. $first_name. The -> is called Pointer. So the output will be:
First Name:Alex
C. Special Data Types
1. PHP NULL Value
The PHP NULL Data type stores only one value i.e NULL itself. When variable do not have any assigned value, It has NULL value. In PHP, Variable can be made empty by assigning NULL value to it.
IMPORTANT: When variable is declared and no value stored, it is automatically assigned a value of NULL.
Example 1of PHP NULL Data Type:
<?php
$two = NULL;
var_dump($two);
?>
The Output will be:
NULL
Example 2 of PHP NULL Data Type:
<?php
$one = NULL;
var_dump($a);
echo "<br>";
$two = "I am String";
$two = NULL;
var_dump($b);
?>
The Output will be:
NULL
NULL
2. PHP Resource
IN PHP, Resource data type is special variable and it holding a reference to an external resource like function or database.
For Example, when you open any file with PHP fopen() then it returns reference to opened file, it is nothing but Resource.
Another Example, when you define connection of PHP script with Database, the mysqli_connect() returns reference.
I would acknowledge you sharing your individual experiences about "PHP 7 Data Types". If you have any doubt/query related to PHP or Web Technologies, Please let me know in Comment Section.
I will certainly take care that, I should give reply for every single comment on my blog posts. So, let’s fire the discussion up! See you in the comments section below..
Sharing is caring ❤️
Thank you for Visiting PHPBash. Keep Visiting for more Interesting concepts of Php language and web technologies from basics to advanced. As well we will also share some ready-to-use, useful code snippets for beginners to kick start their web development project.
Comments
Post a Comment
Thank you so much.