Variables & Data Types in PHP

A Variable is simply a container i.e. used to store both numeric and non-numeric information. Variable is the name of a memory location.

Rules for variable declaration in PHP :

  1. A variable starts with the $ sign, followed by the name of the variable.
  2. The variable name must begin with a letter or the underscore character.
  3. A variable name cannot start with a digit and should not contain space.
  4. A variable can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  5. Variable names are case-sensitive ($age and $AGE are two different variables).

Example :

<?php 
$a=5;
$txt="Hello World!";
$b=20.8;
?>

After the execution of the above statements, the variable $a will hold the value 5, the variable $b will hold value 20.8, and the variable $txt will hold value Hello World!.

Notes:

  1. When you assign a text value to a variable, put quotes around the value.
  2. PHP has no command for declaring a variable. It is created the moment you first assign a value to it.

Output Variables :

In PHP the echo statement is used to output data to the screen.

The following example will show how to output text and a variable.

<?php 
$a=5;
echo $a;
$txt="Hello World!";
echo $txt;
$b=20.8;
echo "Value of variable b is " .$b;
?>
<?php 
$a=5;
$b=10;
echo $a+$b;
?>

PHP Data Types

Variable does not need to be declared its data type adding value to it.

PHP is a Loosely Typed Language so here no need to define the data type.

To check only data type use gettype() function.

To check value, data type, and size use var_dump() function.

Variable can store data of different types, and different data types can do different things.

PHP supports the following data types:

  • String
  • Integer
  • Float / Double
  • Boolean
  • Array
  • Object
  • Null
  • Resource
String:

A string is a sequence of characters. A string can be any text inside quotes. You can use single or double-quotes.

Example : 

<?php
$a="Hello world!";
$b='Hello world!';

echo $a;
echo "<br>";
echo $b;
?>
Integer:

An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.

Rules for integers:

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

Example : 

<?php
$a=1234;
var_dump($a);
?>
Float / Double:

A float (a floating-point number) is a number with a decimal point or a number in exponential form.

Example : 

<?php
$a=20.234;
var_dump($a);
?>
Boolean:

A Boolean represents two possible states: TRUE or FALSE.

$a=true;
$b=false;
Array:

An array stores multiple values in one single variable.

Example :

<?php
$colors=array("red","green","yellow","orange","pink");
var_dump($colors);
?>

In the above example, $colors is an array. The PHP var_dump() function returns the data type and value.

Object:

An object is a data type that stores data and information on how to process that data.

In PHP, an object must be explicitly declared.

Example : 

<?php
class Colors{
    function Colors(){
        $this->color="red";
    }
}

//create an object
$obj=new Colors();

//show object properties
echo $obj->color;
?>

In the above example, first, e must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods.

NULL Value:

Null is a special data type that can have only one value: NULL. A variable of data type NULL is a variable that has no value assigned to it.

Tip: If a variable is created without a value, it is automatically assigned a value of NULL.

Example

<?php
$a="Hello world!;
$a=null;
var_dump($a);
?>
Resource:

The special resource type is not the actual data type. It is the storing of a reference to functions and resources external to PHP.

A common example of using the resource data type is a database call.

Example : 

<?php
$con = mysqli_connect("localhost","root","","users");
?>

The function will return a resource type data to be stored into $con variable.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.