PHP Data Types

PHP Data Types

PHP data Types

Data Type

The data type defines a set of values and set of operations on those
values.

In PHP there are following data types

  • Integers
  • Strings
  • Float/Double
  • Array
  • Boolean
  • Object
  • Null
  • Resources

PHP Integers

The data type which is used to store a whole number with no fractional part is called Integers Data type, (…,-3,-2,-1,0,1,2,3,… ) such as 54,320 etc. Integers can be specified in decimal (base 10), hexadecimal (base 16 – prefixed with 0x), octal (base 8 – prefixed with 0) notation, or binary (base 2) notation optionally preceded by a sign (- or +).binary digits

Syntax

$ Var_Name = Int_Value;

$ marks = 97;

Example

<!DOCTYPE html>
<html>
<body>

<?php
$x = 55;

echo $x.’ is an integer.’ ;
?>

</body>
</html>

Output:

 

55 is an integer.

 

PHP strings

A string is a sequence or a combination of multiple characters. Like “Hello Smith”.A type of data used to store string values is known as String data type. It may include uppercase , lowercase, special symbols and space.

Syntax

$var_Name = “String”;
OR
$Var_Name = ‘String’;

Example

<!DOCTYPE html>
<html>
<body>

<?php
$x = “Display using double quotation marks!”;
$y = ‘Display using single quotation marks!’;

echo $x. “<br>”;

echo $y;
?>

</body>
</html>

Output:

Display using double quotation marks!
Display using single quotation marks!

 

PHP Float / Double

Float data type is used to store real values. Floating point number is a number in exponential form or with a decimal point.

Syntax

$Var_Name = decimal_number;

$Var_Name = Exponential_int;

Example

<!DOCTYPE html>
<html>
<body>

<?php
$a = 1.432;
$b = 1.4e3;

echo $a.’ is Floating-point number. <br>’;
echo $b. ‘ is an Exponential integer.’;
?>

</body>
</html>

Output:

 

1.432 is Floating point number.
1400 is Exponential integer.

 

PHP Array

An Array is a type of data structure that allows to store multiple elements of similar data type in a single variable. The elements of that variable consists of different memory locations. These locations are adjacent in memory and are indicated with same name.

Syntax

$Array_Name = array( Elements_of_Array );

 

PHP Boolean

Boolean is a data type that represents only two possible values (TRUE or FALSE). It is used
in control structure.

 

PHP Object

An Object is a PHP data type that allows storing data and information on, how to process that data. OBJECT is an instance of a class. An object consists on methods and properties to make a particular
type of data useful.

 

PHP Null

PHP Null is a special type of data that is used in PHP to represent empty variables. A Null type of variable has no value to assigned it.

Example

<!DOCTYPE html>
<html>
<body>

<?php
$a = NULL;
var_dump($a);
echo “<br>”;

$b = “Practicing Null Data type!”;
$b = NULL;
var_dump($b);
?>

</body>
</html>

 

PHP Resource

A resource is a special data type that stores references of functions and external resources to PHP.
The most common example of resource data type is database connection .

To learn about PHP Variables CLICK HERE.

Leave a Reply

Your email address will not be published.