PHP

Data Types in PHP

PHP supports data types are-

  • boolean
  • integer
  • float (floating-point number, aka double)
  • string
  • array
  • object
  • resource
  • NULL

Booleans-

This is the simplest type. A boolean expresses a truth value. It can be either TRUE or FALSE.

Example-

<?php
$isLogout = True; // assign the value TRUE to $isLogout
?>

Integers-

An integer is a number of the set ℤ = {..., -2, -1, 0, 1, 2, ...}.
Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation. The negation operator can be used to denote a negative integer.

Example-

<?php
$num = 1234; // decimal number
$num = -123; // a negative number
$num = 0123; // octal number (equivalent to 83 decimal)
$num = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$num = 0b11111111; // binary number (equivalent to 255 decimal)
?>

Floating point numbers-

Floating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes:-

Example-

<?php
$x = 1.234;
$y = 1.2e3;
$z = 7E-10;
?>

Note- The size of a float is platform-dependent, although a maximum of approximately 1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format).

Strings-

A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support.
A string literal can be specified in four different ways:

  • single quoted
  • double quoted
  • heredoc syntax
  • nowdoc syntax

Example- 


<?php
echo 'this is a simple string single quoted.'; // single quoted


echo "this is example of double quoted"; // double quoted

/************* Heredoc string quoting example **************/
$str = <<<EOD
Example of string using heredoc syntax.
EOD;

/******************** Nowdoc string quoting example *************/
echo <<<'EOD'
Example of string spanning multiple lines
using nowdoc syntax. Backslashes are always treated literally,
e.g. \\ and \'.
EOD;
?>

Arrays-

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
The key can either be an integer or a string. The value can be of any type. 

Example-

<?php
$array = array(
"name" => "robert",
"email" => "sample@gmail.com",
"country" => "USA",
);

// Indexed arrays without key

$array = array("php", "swift", "javascript", "erlang");
var_dump($array);

array(4) {
[0]=>
string(3) "php"
[1]=>
string(5) "swift"
[2]=>
string(10) "javascript"
[3]=>
string(6) "erlang"
}
?>

Objects-

To create a new object, use the new statement to instantiate a class. 

Example-

<?php
class person
{
function say_hello()
{
echo "say hello";
}
}

$pObj = new person;
$pObj-> say_hello();
?>

Resources-

A resource is a special variable, holding a reference to an external resource such as opened files, database connections, image canvas areas etc.

NULL-

The special NULL value represents a variable with no value. NULL is the only possible value of type null.

A variable is considered to be null if:

  • it has been assigned the constant NULL.
  • it has not been set to any value yet.
  • it has been unset().

Example-

<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>

 

Back to Top