Data Types in PHP

Estudies4you
Data Types in PHP

Data Types in PHP

PHP supports eight primitive data types to construct variables. The data types supported by PHP are:

Data Types in PHP

Integers
  • Integers can be specified in either decimal, hexadecimal, octal, or binary notation. Integers can be optionally be preceded with +/- signs
  • To use octal notation, precede the number with 0
  • To use hexadecimal notation, precede the number with Ox
  • To use binary notation, precede the number with Ob
Note: Binary integer literals are only available since PHP 5.4.0.

Example:
<?php
$a = 1234; // decimal number
$a = -123; // a negative number $a = 0123; // octal number (equivalent to 83 decimal)
$a = Ox1A; // hexadecimal number (equivalent to 26 decimal)
?>


Doubles
  • Doubles are floating point numbers. Doubles are also known as floats or real numbers
Example:
<?php
$a = 1.234;
$b = 1.2e3;
$c = 7e-10;
?>

Booleans
  • Booleans can have only either of the two values, TRUE or FALSE
Example:
<?php $t=date("H");
if ($t<"18")//$t<"18" can either be TRUE or FALSE depending on time
{
echo "Have a good day!";
}
else
{
echo "Have a good night!";
}
?>

NULL
  • NULL is a special type that has only one value NULL
Example:
<?php
$var = NULL;
?>

Strings
  • Strings are sequence of characters. The can be denoted using either double quotes or single quotes.
Example:
<?php
$txt="Hello world!";
echo $txt;
$my_name = 'Raju';
?>

Arrays
  • Arrays are named and indexed collections of other data types.
Example:
<?php
$names=array("raju","ravi","pavan");
echo "My name is: " . $names[0] . ", " ."</br>"."My friends' names are: ". $names[1] . " and " . $names[2] . ".";
?>

Objects
  • Objects are instances of programmer-defined classes and can package data types and functions that are specific to a class. 9 An Object of a class is created using the new statement.
Example:
<?php
class Myclass
{
function display()
{
echo "welcome to object creation";
}
}
$one = new Myclass;
$one->display();
?>

Resources
  • Resources are special variables that hold references to resources that are external to PHP.
Example:
fbsql_db_query() //Selects a database and executes a query on it.
ftp_connect() //opens an FTP connection to the specified host .
imap_open() //Open an IMAP stream to a mailbox
dba_popen() //establishes a persistent database instance for path with //mode using handler
imagerotate() //Rotate an image with a given angle
To Top