PHP – Variable Types

PHP – Variable Types

Hope this article finds you well. Today we will learn about the PHP variables which are like basic building blocks of PHP.

So read the article till the end and i will make sure you know everything about PHP Variables.

Before i proceed, i would like to request you to do SUBSCRIBE at ShoutMe360.com so that you can get regular updates.

So lets start…..

What is PHP Variable?

Variables are like “containers” to store information.

As told above to store any information in between the PHP program, we need something that can hold that information. Here comes the use of Variables. PHP has different types of variable but before that we need to know some important things about the variables in PHP.

Variable Naming

  • PHP variables start with the $ sign, followed by the name of the variable.
  • PHP variables name must start with a letter or the underscore character.
  • PHP variable names are case-sensitive ($age and $AGE are two different variables).
  • PHP variable name cannot start with a number.
  • PHP variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) but you cannot use characters like + , – , % , ( , ) . & , etc.

PHP is a Loosely Coupled Language

PHP provides great flexibility to the users like PHP automatically converts the variable to the correct data type, determined by its value while in the other languages like C, C++, Java etc., the programmer must declare the type of the variable before using it.

PHP has a total of eight variable types −

Integer − These are the whole numbers means without a decimal point, like 360.

Double − Theses are floating-point numbers, like 1.414 or 3.60.

Boolean − This is the Simplest type of PHP Variable. A Boolean expresses a truth value. It can be either TRUE or FALSE.

NULL − This is a special variable type that only has one value: NULL.

String − These variable holds the sequences of characters, like ‘This is blogging website’.

Array − These are the variables, stores one or more similar type of values in a single value. e.g If you need to store 20 values, in that case, instead of defining 20 different variables it would be better to have a single array variable of 20 length.

Object − These are the instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.

Resources − These are the special variables that hold references to external resources e.g database connections.

Above first five are the simple type, and next two i.e. array and object, are compound.

Note: The Compound types are the containers which can hold other arbitrary values of arbitrary type.

I will explain only simple data type in this article. Array and Objects will be explained in separate article.

Integers

These are the whole numbers, without a decimal point, like 360. These are the simplest type of variable which correspond to simple whole numbers, both positive and negative. Integers can be assigned to variables directly, or can be used in expressions, like below −

$int_var = 360;
$another_int = -360 + 360;

Doubles

These are floating point numbers like 1.414 or 3.60. By default, doubles print with the minimum number of decimal places needed.  or in simple words, A Double is a number with a decimal point or a number in exponential form. For example, the code −

<?php
$float = 2.2888800;
$float2 = 2.2111200;
$floatall = $float + $float2;

print(“$float + $float2 = $floatall<br>”);
?>

Code above will produce the below browser output −

2.28888 + 2.21112 = 4.5

Boolean

These variables holds the value, from two possible values either TRUE or FALSE. PHP provides a couple of constants especially for use as Boolean: TRUE and FALSE, which can be used like so −

if (TRUE)
print(“This will fly.<br>”);
else
print(“This will never fly.<br>”);

NULL

NULL is a special type of variable that only has one value: NULL. To give a variable the NULL value, simply assign it like this −

$null_var = NULL;

A variable that has been assigned NULL has the following properties −

It evaluates to FALSE in a Boolean context.
It returns FALSE when tested with Isset() function.

Strings

These are the sequences of characters, like “ShotMe360 is a blogging Website”. Following are valid examples of string

$str_1 = “This is a string in double quotes”;
$str_2 = ‘This is a single quoted string’;
$str_0 = “”; // a string with 0 characters

Now as you have seen above, we have defined string with two different types, single quoted and double quoted. There is difference between both types of strings.

For the time being you can understand the both type as, Single quoted strings are treated almost as it is, whereas double quoted strings parsed, replace variables with their values as well as specially interpreting certain character sequences.

I will explain both in details in other article.

<?php
$website = “ShoutMe360”;
$string = ‘$website is a blogging website’;

echo $string ;
echo “<br>”;

$string = “‘$website is a blogging website”;
echo $string ;
?>

This will produce following result −

$variable is a blogging website
ShoutMe360 is a blogging website

The escape-sequence replacements are −

  • \n is replaced by the newline character.
  • \r is replaced by the carriage-return character.
  • \t is replaced by the tab character.
  • \$ is replaced by the dollar sign itself ($).
  • \” is replaced by a single double-quote (“).
  • \\ is replaced by a single backslash (\).

PHP Variables Scope

In PHP, variables can be declared anywhere in the program either top, bottom or in between.

The scope of a variable is the context within which it is defined.

PHP has three different variable scopes:

local
global
static

Local Scope:

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:

<?php
function printString() {
$x = 5; // local scope
echo “<p>Variable x inside function is: $x</p>”;
}
printString();

// using x outside the function will generate an error
echo “<p>Variable x outside function is: $x</p>”;
?>

Global Scope:

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:

<?php
$x = 5; // global scope

function printString() {
// using x inside this function will generate an error
echo “<p>Variable x inside function is: $x</p>”;
}
printString();

echo “<p>Variable x outside function is: $x</p>”;
?>

But you can access the global scope variables in the function via a special keyword “GLOBAL”.

With the global keyword, programmer can access a global variable from within a function.

To do this, use the GLOBALS keyword before the variables-

<?php
$integer1 = 5;
$integer2 = 10;

function add() {
$GLOBALS[‘integer2 ‘] = $GLOBALS[‘integer1 ‘] + $GLOBALS[‘integer2 ‘];
}

add();
echo $integer2 ; // outputs 15
?>

PHP The static Keyword

When a function gets fully executed, all of its variables are also gets deleted. but sometimes there is situation when we want a local variable NOT to be deleted as we need to use it for further job.

To do this, use the static keyword when you first declare the variable:

<?php
function printMe() {
static $x = 0;
echo $x;
$x++;
}

printMe();
printMe();
printMe();
?>

 

That’s it!!!

This is Lalit signing off for today. I will catch you on the next Monday with new topic in PHP.

If you like this post please give a thumbs up and keep it sharing.

Hey don’t forget to SUBSCRIBE please so that you can get notification on new posts.

Tada

Leave a Reply