Sri Krishna Arts And Science College
Department of ICT
Course Title: PHP & MYSQL
Course Code: 19CSS22
Class : III BSc – Computer Technology “A”
Google Class room Code: 7cxe5v3
Online Material
Lecture 2: Data types in PHP
Facilitator
[Link]
Assistant Professor
Department of Computer Technology
SKASC
Objective
Data types
Integers
Floating-Point Numbers
Strings
Booleans
Arrays
Objects
Resources
Callbacks
NULL
Variables
Text Books
1. Rasmus Lerdorf, Kevin Tatroe, Bob Kaehms, RicMcGredy
(2020), Programming PHP, O’REILLY(SPD), 4th Edition.
2. Vikram Vaswani, 2017, “PHP A Beginners Guide”, Tata
McGrawHill
3. Lee Babin, Nathan A. Good, Frank M. Kromann, Jon
Stephens (2013), “PHP 5Recipes, A problem solution
approach”, après
DATA TYPES IN PHP
Topic: Data types in PHP
Reference: Clever Techie Channel
DATA TYPES IN PHP
Topic: Data types in PHP
Reference: ChidresTechTutorials
Literals
A literal is a data value that appears directly in a program
Examples
2001
0xFE
1.4142
"Hello World"
'Hi'
true
null
Identifiers
Identifiers are the names of variables.
The names of functions,constants and classes are also
identifiers.
Rules:
Identifiers can be of any length and can consist of letters,
numbers, and underscores.
Identifiers cannot begin with a digit.
Identifiers
In PHP, identifiers are case sensitive. $tireqty is not the same
as $TireQty.
Trying to use them interchangeably is a common
programming error.
Function names are an exception to this rule: Their names
can be used in any case.
A variable can have the same name as a function.
This usage is confusing, however, and should be avoided.
Also, you cannot create a function with the same name as
another function.
Identifiers
Identifiers can be any length.
This is advantageous because it enables a programmer to
accurately describe the identifier's purpose via the identifier
name
An identifier name can't be identical to any of PHP's
predefined keywords.
Variables
▪ PHP variable is just a assigment to refer a location in memory
where some value is stored.
▪ You can not refer to memory addresses directly.
▪ Since PHP variable friendly refered to memory locations.
▪ All variables in PHP are prefixed with a dollar sign ($).
▪ PHP parser to recognize the variable dollar sign and dollar
sign is not technically part of the variable name.
▪ PHP variables most of part have a single scope, which most
used included and required files as well.
Variables-Example
<?php
$b = 1; /* global scope */
function test()
echo $b; /* reference to local scope variable */
}
Any variable used inside a function is
called local function scope.
test(); Note: This example will not produce any
output because echo statement refers to
?> a local version of the $b variable and it
has not been assigned a value within this
scope.
DATA TYPES IN PHP
Data Types in PHP
PHP data types are used to hold different types of data or
values.
PHP supports 8 primitive data types that can be categorized
further in 3 types:
Scalar Types (predefined)
Compound Types (user-defined)
Special Types
Data Types in PHP
PHP Data Types: Scalar Types
It holds only single value. There are 4 scalar data types in
PHP.
boolean
integer
float
string
Data Types in PHP
PHP Data Types: Compound Types
It can hold multiple values. There are 2 compound data types
in PHP.
array
object
PHP Boolean
Booleans are the simplest data type works like switch.
It holds only two values:
TRUE (1)
FALSE (0).
It is often used with conditional statements.
If the condition is correct, it returns TRUE otherwise FALSE.
<?php
if (TRUE)
echo "This condition is TRUE.";
if (FALSE)
echo "This condition is FALSE.";
?>
Output:This condition is TRUE.
PHP Integer
Integer means numeric data with a negative or positive sign.
It holds only whole numbers
Rules for integer:
An integer can be either positive or negative.
An integer must not contain decimal point.
Integer can be decimal (base 10), octal (base 8), or
hexadecimal (base 16).
The range of an integer must be lie between 2,147,483,648
and 2,147,483,647 i.e., -2^31 to 2^31.
PHP Integer-Example
<?php
$dec1 = 34;
$oct1 = 0243;
$hexa1 = 0x45;
echo "Decimal number: " .$dec1. "</br>";
echo "Octal number: " .$oct1. "</br>";
echo "HexaDecimal number: " .$hexa1. "</br>";
?>
Output:
Decimal number: 34
Octal number: 163
HexaDecimal number: 69
PHP Integer-Example
243)8 = (163)10
Step by step solution
Step 1: Write down the octal number:
243
Step 2: Multiply each digit of the octal number by the
corresponding power of eight:
2x82 + 4x81 + 3x80
Step 3: Solve the powers:
2x64 + 4x8 + 3x1
Step 4: Add up the numbers written above:
128 + 32 + 3 = 163
So, 163 is the decimal equivalent of the octal number 243.
PHP Integer-Example
To convert the hex number 0X45 to decimal (or any other
hexadecimal number for that matter), you follow these steps:
Step 1) Multiply the last digit by 1, Multiply the second to last digit
by 16, Multiply the third to the last digit by 16 x 16, Multiply the
fourth to the last digit by 16 x 16 x 16, Multiply the fifth to the last
digit by 16 x 16 x 16 x 16 and so on until all the digits are used.
Step 2) Add up all the products you got from Step 1 to get the
answer to 0X45 in decimal.
5x1=5
4 x 16 = 64
5 + 64 = 69
Float/double Data type-Example
It is also called numeric data types.
A number with a fractional component.
$num hold value=100.0.
pass $num inside echo statement to display the
output.
<?php
$num=100.0;
var_dump($num);
?>
Output: float(100)
Float/double Data type-Example
<?php
$n1 = 19.34;
$n2 = 54.472;
$sum = $n1 + $n2;
echo "Addition of floating numbers: " .$sum;
?>
Output: Addition of floating numbers: 73.812
PHP String
A string is a non-numeric data type. It holds letters or any
alphabets, numbers, and even special characters.
String values must be enclosed either within single quotes or in
double quotes. But both are treated differently.
<?php
$company = "Javatpoint"; Hello Javatpoint
Hello $company
echo "Hello $company";
echo "</br>";
echo 'Hello $company';
?>
PHP Array
• An array is a compound data type.
• It can store multiple values of same data type in a single
variable.
<?php
$bikes = array ("Royal Enfield", "Yamaha", "KTM");
var_dump($bikes); echo "</br>";
echo "Array Element1: $bikes[0] </br>";
echo "Array Element2: $bikes[1] </br>";
echo "Array Element3: $bikes[2] </br>";
?>
PHP Array-Output
Output
array(3) { [0]=> string(13) "Royal Enfield"
[1]=> string(6) "Yamaha" [2]=> string(3)
"KTM" }
Array Element1: Royal Enfield
Array Element2: Yamaha
Array Element3: KTM
PHP object
Objects are the instances of user-defined classes that can
store both values and functions.
They must be explicitly declared.
<?php
class bike {
function model() {
$model_name = "Royal Enfield";
echo "Bike Model: " .$model_name;
}
}
Bike Model: Royal Enfield
$obj = new bike();
$obj -> model();
?>
PHP Resource
Resources are not the exact data type in PHP.
Basically, these are used to store some function calls or
references to external PHP resources.
For example - a database call. It is an external resource.
<?php
$con = mysqli_connect("localhost","root","","users");
?>
Function will return a resource type data to be stored into
$con variable.
PHP Null
Null is a special data type that has only one value: NULL.
There is a convention of writing it in capital letters as it is
case sensitive.
The special type of data type NULL defined a variable with
no value.
<?php
$nl = NULL;
echo $nl; //it will not give any output
?>
Predefine functions to Check data type
is_int( ) : Check given value is integer or not
is_float( ) : Check given value is float or not
is_numeric( ) : Check given value is either integer or float
is_string( ) : Check given value is string or not
is_bool( ) : Check given value is Boolean or not
is_array( ) : Check given value is array or not
is_object( ) : Check given value is object or not
is_null( ) : Check given value is null or not
Variables Types
Variable Variables
You can reference the value of a variable whose name is
stored in another variable.
Ex: $foo = 'bar';
$$foo = 'baz';
After the second statement executes, the variable $bar has
the value "baz".
Variables Types
Variable References
In PHP, references are how you create variable aliases.
To make $black an alias for the variable $white,
use: $black =& $white;
The old value of $black is lost. Instead, $black is now another
name for the value that is stored in $white
Variables Types
Variable References
After the assignment, the two variables are alternate names
for the same value.
Unsetting a variable that is aliased does not affect other
names for that variable's value, though:
$white = "snow";
$black =& $white;
unset($white);
print $black;
Output: snow
Variable Scope
There are four types of variable scope in PHP:
local
global
static
function parameters.
Local Scope
A variable declared in a function is local to that function.
It is visible only to code in that function (including nested
function definitions);
it is not accessible outside the function.
function update_counter ( )
{ $counter++; }
$counter = 10;
update_counter( ); Output: 10
echo $counter;
Global Scope
Variables declared outside a function are global.
Can be accessed from any part of the program.
However, by default, they are not available inside functions.
To allow a function to access a global variable, you can use
the global keyword inside the function to declare the variable
within the function
function update_counter ( )
{ global $counter;
$counter++; }
Output: 11
$counter = 10;
update_counter( );
echo $counter;
Static Variables
A static variable retains its value between calls to a function
but is visible only within that function.
You declare a variable static with the static keyword.
function update_counter ( )
Output:
{
Static counter is now 1
static $counter = 0; Static counter is now 2
$counter++; Global counter is 10
echo "Static counter is now $counter\n";
}
$counter = 10;
update_counter( );
update_counter( );
echo "Global counter is $counter\n";
Function Parameters
Function parameters are local, meaning that they are available
only inside their functions.
function greet ($name)
{
Output:
echo "Hello, $name\n";
Hello, Janet
}
greet("Janet");
Note: In this case, $name is inaccessible from outside greet( ).
Garbage Collection
To understand memory management in PHP, you must first
understand the idea of a symbol table .
There are two parts to a variable
its name (e.g., $name), and
its value (e.g., "Fred").
A symbol table is an array that maps variable names to the
positions of their values in memory.
When you copy a value from one variable to another, PHP
doesn't get more memory for a copy of the value.
Garbage Collection
Instead, it updates the symbol table to say "both of these
variables are names for the same chunk of memory."
So the following code doesn't actually create a new array:
$worker = array("Fred", 35, "Wilma");
$other = $worker; // array isn't copied
If you then modify either copy, PHP allocates the memory and
makes the copy:
$worker[1] = 36; // array is copied, value changed
By delaying the allocation and copying, PHP saves time and
memory in a lot of situations. This is copy-on-write.
Garbage Collection
Reference counting is the preferred way to manage memory.
Keep variables local to functions, pass in values that the
functions need to work on, and let reference counting take
care of freeing memory when it's no longer needed.
If you do insist on trying to get a little more information or
control over freeing a variable's value, use the isset( ) and
unset( ) functions.
Garbage Collection
To see if a variable has been set to something, even the empty
string,
use isset( ):
$s1 = isset($name); // $s1 is false
$name = "Fred";
$s2 = isset($name); // $s2 is true
Use unset( ) to remove a variable's value:
$name = "Fred";
unset($name); // $name is NULL
Assignment
Check if given variable holds a integer type of value then print
the sum otherwise show error message
Is there any limit on how large a PHP integer value can be?
Do It Yourself
Click
THANK YOU