DATA TYPES OF PHP
Glimpses of previous lecture:
In the last lecture we have discussed
about :
Introduction of php
Features of php
Basic php syntax
Commenting the php code
In the current lecture
We will learn about
All about php data types
PHP Data Types
Data Types define the type of data a variable
can store. PHP allows eight different types of
data types.
There are Scaler(pre-defined),
compound(user-defined), and special data
types.
PHP is a dynamic, loosely-typed language,
which means that variables do not need to be
declared with a specific data type.
Data types which are supported in php
The predefined data types are:
• Boolean
• Integer
• Double
• String
The user-defined (compound) data
types are:
• Array
• Objects
The special data types
are:
• NULL
• Resource
The first five are called simple data types and
the last three are compound data types.
Scalar data types are also known as pre-
defined data types and compound data
types are also called user-defined data
types
First we will Discuss About primitive data type
In PHP, scalar types are those data types that
can hold only one value at a time.
They are called scalar because they
represent a single value, as opposed to
compound types that can store multiple
values
INTEGERS:
An integer data type is a non-decimal number
An integer must have at least one digit
An integer must not have a decimal point
An integer can be either positive or negative
Integers can be specified in: decimal (base
10), hexadecimal (base 16), octal (base 8), or
binary (base 2) notation
For example:
<?php
$x = 67;
?>
BOOLEAN:
It hold two values: True for successful event
and False for unsuccessful events.
$x = true;
$y = false;
FLOAT:
Also known as double
a number with a decimal point or a number in
exponential form
it can hold numbers containing fractional or
decimal parts.
For example:
STRINGS
A string is a text inside the quotes.
Quotes can be single or double quotes.
Hold letters or any alphabets, even numbers
are included. These are written within double
quotes during declaration. The strings can
also be written within single quotes but will be
treated differently while printing variables.
STRINGS
<?php
$name = “Mamta";
echo "$name \n";
echo My name is $name ';
echo "\n\n";
var_dump($name)
?>
ARRAY
Array is a user defined data type
It can store multiple values of a data type
Array is a compound data type that can store
multiple values of the same data type
<?php
$intArray = array( 10, 20 , 30);
echo "First Element: $intArray[0]\n";
echo "Second Element: $intArray[1]\n";
echo "Third Element: $intArray[2]\n\n";
var_dump($intArray);
?>
// Indexed array
$fruits = ['apple', 'banana', 'orange'];
echo $fruits[0];
// Associative array
$person = ['name' => 'John', 'age' => 30,
'location' => 'New York'];
echo $person['name'];
OBJECT
As we know object is an instance of a class
Before explaining object data type we have to
define a class
. When an object is created, they inherit all
the properties and behaviours from the class,
having different values for all the properties.
objects are explicitly declared and created
from the new keyword
<?php
class color {
var $carcolor
function color($carcolor) {
$this->message = $carcolor;
}
function m () {
return "This car is of " . $this->message . “color!";
}
}
// instantiating a object
$newObj = new color(“black");
echo $newObj -> msg();
?>
Resource
The special resource type is not an actual
data type. It is the storing of a reference to
functions and resources external to PHP.
For example, consider a database call. This is
an external resource. Resource variables hold
special handles for files and database
connections.
NULL
These are special types of variables that can
hold only one value i.e., NULL.
We follow the convention of writing it in
capital form, but it’s case-insensitive NULL,
Null, and null are treated the same.
If a variable is created without a value or no
value, it is automatically assigned a value of
NULL. It is written in capital letters
<?php
$n = NULL;
echo $n;
var_dump($n);
?>
Things to remember
To check the type and value of an expression,
use the var dump() function which dumps
information about a variable.
In next class
Php variables