pTutorial

New way to learn PHP

PHP 5 Data types

Data type

Most of other languages like C/C++ and Java require specifying what kind of data it can hold, but in PHP there is no need to specifying data type before using it. All the data store in PHP variable. Data types are the backbone of any programming language.

Example of data type

<?php

$test=15.021;
$test1=38.35;
$sum=$test*$test1;
echo "First value=".$test."
"; echo "Second value=".$test1."
"; echo "Result=".$sum; ?>

In this example $test, $test1 are the float type variable, after multiplying the $test and $test1 store the value into $sum variable. You can use gettype function to check the data type of the variable.

Data types in PHP under various categories are shown below

  • String
  • Integer
  • Floating point numbers
  • Boolean
  • Array
  • Object
  • NULL

Hierarchy of data type

  1. Scalar Data Type
  2. Compound Data Type
  3. Special Data Type
Data types

Scalar Data Type

  • Scalar data means data that contains only a single value.
  • There are four scalar data types.

All are scalar data types are following below.

Scalar data type Description Example
Integers A whole number $a =38;
Float A floating point number $a=5.50;
Strings Sequence of character $a="string";
Boolean Either true or false $a=true;

Integer

An integer is an integral whole number without a decimal point. Integer numbers are used for counting purposes, like 38, 8 etc.

Float

Float variables hold Fractional, like 38.025, 7.652 etc. It's used for banking and financial sector calculation.

String

A string is defined as a character array, like John, car etc. It's used for storing the array of character.

Boolean

Boolean variables hold true or false.

All of scalar data type illustrate with example below

<?php

$test = 7;
$test1 = 38;
$sum = $test+$test1;
echo "First value=".$test."
"; echo "Second value=".$test."
"; echo "sum=".$sum."
"; $test2 = "john "; $test3 = "maclen"; echo "$test2"."$test3"."
"; $a = 35.25; $b = 65.32; $diff = $b-$a; echo "difference=".$diff; ?>

Example of boolean data type

 <?php

$a=true;
if($a==true)
{
echo " value of variable a is ture";	
}
else
{
echo"value of variable a is not ture";	
}

?>
 

Output

value of variable a is ture

In the above example $a is not a string variable, it's a boolean variable and hold the value "true". So that the result will be "value of variable a is true".

Compound data types

PHP supports two compound types, Compound data is data that can contain more than one value.

All Scalar Data Types listed below

Compound data type Description
Array Its holds multiple values
Object A type that may contain properties and methods

Compound data type example

<?php
$numbers = array( 5,8,45,6);
foreach( $numbers as $value )
{
  echo "Value is $value 
"; } $numbers[0] = "John"; $numbers[1] = "apple"; $numbers[2] = "car"; foreach( $numbers as $value ) { echo "Value is $value
"; } ?>

In this example $number is an array variable that holds some integer value, you can access all the value of an array by using a foreach loop as shown above.

Special Data types

Null Data Type

  • It may contain null as a value.
  • It is a special data type.
<?php

$Var=NULL;  
if (!$Var)  
{  
echo "Welcome to the  php
"; echo "Welcome to the php"; } ?>

NULL is not a string it is a special data type that means variable does not have any value, you can check by using if-else.

Check the type of variable

You can use the gettype() function to check the data type of variable, gettype function returns the data type as a string.

Syntax

 
gettype(variable)

Illustrate with example

<?php

$test = array( 5,"umar",45.2,NULL);

foreach( $test as $value )
{

  echo gettype($value)."
"; } ?>

In this example, $test is an array variable that holds the integer, string, double and NULL values respectively. You can use a foreach loop to access array element one by one, pass the $value as an argument in the gettype function and it returns the data type of each element of the array.

Scope of variables

Each variable has an area in which it exists, knows as it is scope

There are two types of scope

  1. Local scope
  2. Global scope

local scope

The function has their own scope, that means the variable used in within a function not available outside the function.

<?php

Function concat() 
{
$a = "Hello , ";
$b = "welcome to php!";
return $a . $b;
}
echo concat();
/*$a cannot used any 
Where out of this function
*/

?>

The variable $a or $b not accessible outside the function concat, it's limited in the in that function, so it's called local variables.

Global variable

All variable set outside the function considered as global that means they can access from anywhere in the script.

<?php

$global = "Welcome to php"."
"; //its global variable function hello() { global $global; echo "$global "."
"; } hello(); // Displays "Welcome to php" echo $global; //its accessible anywhere in PHP script echo $global; hello(); hello(); ?>

Global variable accessible anywhere in PHP script using the global keyword.

Static keyword

  • A static variable will not erase its value when the function terminates.
  • It keeps the previous store value, used if the function again called.

Non static variable


<?php
echo "Non static variable example";
function cout() {
$count = 0;
$count++;
echo "
".$count; } cout(); cout(); cout(); ?>

Output

Non static variable example
1
1
1

In case of non-static variable previous value will lose and initialised with zero again every time count function called.

Example of static variable

<?php

function counta()
 {
STATIC $a = 1;
return ++$a;
}
echo "count: " . counta() . " 
"; echo " count: " . counta() . "
"; echo " count: " . counta() . "
"; ?>

In case of static variable previous value never lost whenever function terminates. It keeps previous value.


QUOTES OF THE DAY

Knowledge without integrity is dangerous and dreadful.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER