PHP – Data Types

All data stored in PHP variables fall into one of eight basic categories, known as data types. A variable ’ s data type determines what operations can be carried out on the variable ’ s data, as well as the amount of memory needed to hold the data.

These data types are as follows-

  • Booleans
  • Integers
  • Doubles/Float
  • Strings
  • NULL
  • Arrays
  • Objects
  • Resources

Booleans

This data type can only hold two types of values, which include TRUE and FALSE. There are situations that demand expressions to be evaluated as boolean giving the result of TRUE and FALSE. While performing such evaluations, the following rules must be kept in mind-

  • If the expression evaluates to a number, which is zero, then the value of the expression is FALSE. Any other number is considered non-zero and is evaluated as TRUE.
  • If the expression evaluates to a string, then the Boolean value of the expression is TRUE if it evaluates to a valid string. On the other hand, if the string is empty, then the expression is evaluated to FALSE.
  • Any expression that evaluates to the value NULL has an equivalent Boolean value of FALSE.
  • If an expression evaluates to an array, then the Boolean value of the expression is TRUE if the array has elements. On the other hand, if the value of the expression is an array with no elements, its Boolean value is FALSE. Considering the case of objects, if any of the member variables of the objects have a value assigned to it, then the object evaluates to the Boolean value TRUE, else it is evaluated as FALSE.
  • Any resource that is valid for the PHP system is evaluated as TRUE.
  • The use of doubles as Booleans must be avoided.
$a=TRUE:
$b=FALSE;

Integers

All the whole numbers are included in Integers. For example, any number like 6758 or 54 is an integer. The syntax or PHP expression for assigning a value to an integer, is given below-

$int_variable=32;

It is important to mention that the largest integer that can be held by a system is 2,147,483,647.

Rules for Integer:

  • An integer must have at least one digit.
  • An integer must not have a decimal point.
  • An integer can be either positive or negative.
  • Integers can be specified in: decimal(base 10), hexadecimal (base 16), octal(base 8), or binary (base 2) notation.

Doubles

All floating point number that are actually just whole numbers followed by decimal point and fractional part fall under this data type.

<?php
$x=1234;
var_dump($x);
echo "<br />";

$y=1.234;
var_dump($y);
?>

Output:
float(1234)
float(1.234)

Strings

Any combination of characters is referred to as a string. All string operations are supported in PHP.

A String can hold numbers, letters and special characters and it can be as large a up-to 2 GB.

<?php
$x='Welcome to PHPGurukul';
echo $x;
echo "<br />";

$y="Welcome to PHPGurukul";
echo $y;
?>

Output:
Welcome to PHPGurukul
Welcome to PHPGurukul

NULL

This data type can store only one value and is usually used for initializing the variable. The one value that is stored is NULL. Although conventionally capital letters are used for writing NULL, it can also be written as ‘null’ and the system will recognize it as the same. Therefore, the following statement is valid-

<?php
$var=null;
?>

There are two important things to note about NULL. First, NULL always evaluates to FALSE when a Boolean equivalent is to be determined.

Secondly, if NULL is tested against IsSet() function, it always evaluates to FALSE.

Arrays

A collection of data of the same type can also be called an array.In fact, arrays can also be visualized as a number of variables of the same type, which are referred by the same name and are distinguished by the index at which they are present.

<?php
$colors=array("Blue","Red","Black");
var_dump($colors);
?>

Output:
array(3) {[0]=>string(4)”Blue”[1]=>string(3)”Red”[2]=>string(5)”Blue”}

Objects

Programmers can define classes or templates, which when instantiated create objects. A class is typically composed of data values and functions. An object of that class can access all the functions of the class ans possesses a set of data members of itself, in accordance with the data members defined for the class.

<?php
//Class
class welcome{
//properties
public $var="Welcome to PHPGurukul";
//method
function show_msg(){
    return $this->var;
}
}

//create object from the class
$welcome_msg=new welcome
var_dump($welcome_msg);
?>

Output:
object(welcome)#1(1){[“str”]=>string(21)”Welcome to PHPGurukul”}

Resources

There may be some programming scenarios that require you to refer to resources that are not available within PHP. An example of such a programming scenario is database connection. In order to hold such references, special are required, which are called resources.

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy