Tuesday, 31 January 2017

DATATYPES IN PHP

Datatypes are used to specify type of variable data. In PHP, datatypes are mainly divided into 3 types –

1. Scalar Datatypes – Boolean, Integer, Float, String
2. Compound Datatypes – Array, Object
3. Special Datatypes – Resource, Null


SCALAR   DATATYPES
This datatype is further divided into 4 different types –

A. Boolean – This data-type can represent either true or false. In PHP value of true is 1 and value of false doesn’t contain any value

Example –
<?php
echo true;
echo false;
?>

Output –
1

is_bool – Using this function we can check input variable is Boolean or not.

Example –
<?php
$x=10;
echo is_bool($x);
$b="abc";
echo is_bool($b);
?>

(bool)var – Converts variable into boolean variable.

Example –
<?php
$b="abc";
$x=(bool)$b;
echo is_bool($x);
?>

Output –
1


B. Integer – This data-type can store numeric value.

is_int / is_integer – To check input variable is integer or not.
(int)var / (integer)var / intval(var) – To convert variable into integer variable.

Example –
<?php
$x="123";
$x=intval($x);
echo is_int($x);
echo "\n";
echo $x;
?>

Output –
1
123

Example –
<?php
$x="12Scott34";
$x=intval($x);
echo is_int($x);
echo "\n";
echo $x;
?>

Output –
1
12

Note – If $x=”Scott1234”, then value of $x=0


C. Float – This data-type can store decimal values.

is_float – This function checks input variable is float or not.
float(var) – Converts a variable into floating point number.

Example –
<?php
$x="12.34";
$x=(float)$x;
echo is_float($x);
?>

Output –
1


D. String – String is a collection of characters. In PHP, we can declare string in 3 ways – by using single quotations, by using double quotations, by using heredoc syntax.

Example –
<?php
echo 'Welcome';
echo "\n";
echo "Welcome";
echo "\n";
$x=100;
echo 'Value is $x';
echo "\n";
echo "Value is $x";
?>

Output –
Welcome
Welcome
Value is $x
Value is 100

Note – Variable in double quotation returns value of variables.

Heredoc – PHP supports heredoc syntax to avoid the conflicts what we are getting with single and double quotations.

Example –
<?php
$str = <<<DEMO
<input type='button' value="Click">
DEMO;

echo $str;
?>



COMPOUND   DATATYPES –

A. Array – Array is a collection of heterogeneous elements.

B. Object – Object is an instance of class.



SPECIAL    DATATYPES –

A. Resource – This data-type can refer external resources like database connections, file pointers etc

Example –
<?php
$x=mysql_connect("localhost","root","");
echo $x;
?>

Output –
Resource id #5


B. Null – In PHP, null is not a value. We can consider a variable as null variable based on 3 conditions
  • ·        If variable is not set with any value

Example –
<?php
$x; // x is not set with any value
echo is_null($x);
?>

Output –
Notice: Undefined variable: x in C:\Users\om\workspace\PhpProjec\first.php on line 3
1

  • ·        If variable is set with null

Example –
<?php
$x=null;
echo is_null($x);
?>

Output –
1

  • ·        If variable’s value is unset

Example –
<?php
$x=100;
unset($x);
echo is_null($x);
?>

Output –
Notice: Undefined variable: x in C:\Users\om\workspace\PhpProjec\first.php on line 4
1


is_null – Using this function we can check input variable is null or not.

No comments:

Post a Comment