Variables in PHP – Explained with Examples

0
661
Variables in PHP

Guys here’s brand new tutorial in our Learn PHP Programming Series. This article will help PHP beginners to learn the concept of Variables in PHP easily. We have inserted code examples to help you in your Assignments or for your notes. Without any further talking, let’s start learning.

What are Variables in PHP Programming Language?

Look, friend, Variables are nothing but just a container, to store values. Example- To make tea at home, we keep different ingredients in different boxes. Like Sugar in one box, Milk in another container etc. Later on, we use these different items in our tea. In the same way, While programming we need to store data before we can process them. You can store Numbers or strings or boolean values in your Variables. As the Name suggests, Variables can vary values (Unlike Constants in PHP, Which can never change their values.)

How to Create Variables in PHP?

If you have programmed before in other programming languages like C C++ or Java – You must have used different types of variables like Integer type, Character type, Float type etc. Here in PHP, We don’t define the type of variable. This is why PHP is called loosely typed language since we don’t tell the type of data we need to store, it converts everything internally.
A variable is prefixed with $ symbol. Make sure you use $ symbol each time you use your variable. (Personal Experience, i programmed for 2 years in C And C++ so one common mistake i made in early days of PHP is this. I always used to miss $ symbols with variables, especially in Loops. So make sure if you have a variable ‘$i’ you must use ‘$i’ each time and not just ‘i’). There are different types of variables in PHP, let’s understand them one by one.

Types of Variables

Variables in PHP explained
Variables in PHP explained
  1. Dynamic Variable
  2. Reference Variable
  3. Local Variables
  4. Global Variables
  5. Superglobal Variables

Dynamic Variable in PHP

dynamic variables in php

<?php
$a="macky";
$$a="Founder at Programmingy";
echo "$a <br>";
echo "$macky";
?>

Dynamic variables are variables’ variable. You can name a variable using another variable stored value name.
For example: We created a normal variable $a and stored macky in it. Then we created our dynamic variable using $$a. What this does is, It creates a dynamic variable by name stored in $a. So, the new variable name will be ‘macky’ and we have initialized it with a text (Founder at programmingy). These types of variable are generally used in SQL Queries when we want to you variables names that match the field names of our database.

Reference Variable in PHP

Reference variables in PHP
Reference variables in PHP
<?php
$a=100;
$b=&$a;

echo "$a ... $b";

$b=$b+100;

echo "<br> $a ... $b";

?>

We can assign one variable reference in another variable. That means, We can access and manipulate one variable using another variable (Using references).

See the example above, We stored 100 in $a. Then we stored the reference of $a in $b using: $b=&$a (Remember to use $ ampersand symbol, which kind of stores the address of $a variable in $b). Then we tried to print, both the variables, $a and $b and the result for both is same i.e., 100. It happened because we stored $a variable reference in $b variable, now $b also points to the same value of $a.
To make things more clear, i added 100 in the previously stored value of $b and then again printed $a and $b. Guess what? 100 was added in original value of $a. After printing $a and $b – We got 200 for both. (I Hope i made this clear, if not comment down below and i will explain further more).

Local Variables in PHP

Third type of Variable, these are some normal kind of variables, which can be accessed only in a block of code (Generally within a function only). They can hold their values (or we can say they exist) only in a function. Lets see some codes below to understand local variables in PHP.

Local Variables in PHP

<?php

function fun(){
 $str= "My Life, My Rules";
 echo $str;
}
echo $str;

?>

We created a variable $str in function fun and tried to access it after closing the function. We couldn’t accessed it.

Local Variables in PHP

Here we simply commented the line of code, where we tried to access $str variable outside the function. Then we called our function and it gave us the expected output. (If you’re not well versed with the concepts of using functions, check out this small tutorial on using Functions in PHP quickly.)

Local Variables in PHP

<?php
$str="Jai Hanuman <br>";
fun();
function fun(){
 $str= "My Life, My Rules <br>";
 echo $str;
}
echo $str;

?>

Here i tried to play with code. I have made 2 variables with the same name $str, One inside function and one outside function. You can see the output, it gave us the expected output.

Global Variables in PHP

Lets see our 4th type of Variable in PHP that is – Global Variable. These variables are accessible to all the source code. They are defined using ‘global’ keyword.

global variables in php
global variables in php
<?php
$a=99; 
function fun(){
 global $a;
 echo $a;
}
fun();
echo "<br>$a";
?>

We made a variable $a then in function fun(), we used our Global Keyword.  Then we simply printed the value of $a and it gave us the desired output. Outside function we again accessed $a and it printed the same result. I know it looks hell simply now, but trust me it has always been.

Superglobal Variables in PHP

5th and last type is superglobal variables, we will not see any example program for superglobal variables in this article, as this is a big and important topic. We will only understand the basics only and will learn more about superglobal variables in upcoming articles.

Superglobal Variable – These are provided by the PHP Environment but are global within the program. They store useful information about currently running program and its environment. They are structured as Associative Arrays. There are 6 types of Superglobal variables including $_GET and $_POST. We will learn about them using example program in coming articles.

I hope i made this thing clear and now you all have a good working knowledge of Variables in PHP. If you liked my work, then please do share this article (I will really appreciate it man). You can also checkout other articles on PHP Programs for beginners.

LEAVE A REPLY

Please enter your comment!
Please enter your name here