LECTURE 2-Data Types
LECTURE 2-Data Types
Department of ICT
Online Material
Lecture 2: Data types in PHP
Facilitator
B.Hemalatha
Assistant Professor
$b = 1; /* global scope */
function test()
}
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
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
<?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
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
<?php
$nl = NULL;
?>
Predefine functions to Check data type
is_int( ) : Check given value is integer or not
$$foo = 'baz';
After the second statement executes, the variable $bar has
the value "baz".
Variables Types
Variable References
$white = "snow";
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.
function update_counter ( )
{ $counter++; }
$counter = 10;
update_counter( ); Output: 10
echo $counter;
Global Scope
Variables declared outside a function are global.
{
Output:
echo "Hello, $name\n";
Hello, Janet
}
greet("Janet");
use isset( ):
$name = "Fred";
$name = "Fred";
Click
THANK YOU