PHP Data Types

PHP Data Types

  • PHP
  • 4 mins read

PHP variable can contain data of different data types.

PHP supported Data types:

  • String
  • Integer
  • Float(floating point numbers)
  • Boolean
  • Array
  • Object
  • Null
  • Resource

PHP string

Actually, a string is a sequence of characters. It is enclosed using single and double quotes.

Example:

<html>
<body>
<?php
$a="well come to Kolkata";
$b='well come to kolkata';
echo $a;
echo $b;
?>
</body>
</html>

Hereafter the execution of two statements $a and $b will contain the text "well come to Kolkata" and through echo statement, the string "well come to Kolkata" will be displayed on the web page for two times.

Integers

An integer can contain the whole numbers such as 1,-2, etc. Integer can be specified in decimal(base 10), hexadecimal(base 16), octal(base 8) or binary(base 2) notation, optionally preceded by a sign(- or +).

To use octal notation, the number is preceded by 0(zero), to use hexadecimal notation, the number is preceded by 0x. To use binary notation, the number is specified by 0b.

Example:

<html>
<body>
<?php
$a=98; //decimal number
$b=056; //octal number
$c=0x56; //hexadecimal number
$d=0b111;//binary number
echo $a;
echo $b;
echo $c;
echo $d;
?>
</body>
</htm>

Float

A float is a number which contains a decimal point or a number in exponential form.It is also known as double.

Example

<html>
<body>
<?php
$a=56.234;
echo $a;
?>
</body>
</htm>

Output:

56.234

Boolean

This data type is used in a conditional expression. It contains two values, one is TRUE(1), and another is FALSE(0).

Program on Boolean

<html>
<body>
<?php
$a=TRUE;
echo $a;
?>
</body>
</htm>

Output:

1

Array in PHP

It is a special type of variable which can contain multiple values at the same time. An array element value can be accessed through the specified index number of the array. In PHP array can be created through the function array();

There are three types of Array in PHP

  1. Indexed Array
  2. Associative Array
  3. Multidimensional Array

Indexed Array: It is an array where the index will be specified by a numeric value. The numeric index will start from 0.

Program on Indexed Array

<html>
<body>
<?php
$a=array(“vinish”,”kolkata”,”90000”);
print_r($a);
?>
</body>
</html>

Output:

Array ( [0] => vinish [1] => kolkata [2] => 90000 )

Here $a[0]=first array element value=”vinish”
Here $a[1]=second array element value=”kolkata”
Here $a[2]=first array element value=”90000”
Print_r is used to print the Array

Program on Indexed Array

<html>
<body>
<?php
$a=array();
$a[0]=”vinish”;
$a[1]=”kolkata”;
$a[2]=”90000”;
print_r($a);
?>
</body>
</html>
Output: Array ( [0] => vinish [1] => kolkata [2] => 90000 )

Here $a[0]=first array element value=”vinish”
Here $a[1]=second array element value=”kolkata”
Here $a[2]=first array element value=”90000”
NB:Array can be created using this way also

Associative Array: It is an array with string index.

Program on Associative Array

<html>
<body>
<?php
$a=array(“name”=>”vinish”,”address”=>”khardah”,”salary”=>”90000”);
echo $a[‘name’];
?>
</body>
</html>

Output:

vinish

Here $a[‘name’]=”vinish”
Here $a[‘address’]=”kolkata”
Here $a[‘salary’]=”90000”

Another way for creating an Associative Array

<html>
<body>
<?php
$a=array();//declaring an array
$a[‘name’]=”vinish”;
$a[‘address’]=”kolkata’;
$a[‘salary’]=”90000”;
echo $a[‘name’];
?>
</body>
</html>

Output:

vinish

NB: Multidimensional array will be discussed later.

NULL data type

The NULL data type is a datatype which contains a null value. A variable of data type null is a variable which does contain any value.

Programs on the NULL data type

<html>
<body>
<?php
$a=null;
var_dump($a);
?>
</body>
</html>
Output: NULL (Here var_dump() is a function which is used to display the value with its type)
NB: There is an example with var_dump()
<html>
<body>
<?php
$a=10;
var_dump($a);
?>
</body>
</html>

Here output will be int(10). Here var_dump($a) will return the value of $a with its data type.

See also: