logo
logo
Sign in

What are Variables and how to use them in PHP?

avatar
Neha ADMEC
What are Variables and how to use them in PHP?

Variables are type of container, which is used to store any type of information, we can also say that it is the name of memory location that is going to hold the data. We can write php within html, to use php we have to use <?php ?> between this opening and closing tag of the php we can write our php code. The code we write in php will not be occurred in the browser, we can’t even see the php code in the page source. To declare a variable in php we have to use a prefix “$”, php variables can hold any type of values either integer or string, Boolean. When we declare the variable in php at that time it doesn’t know about the type of the data it is going to hold, the data will be assigned to the variable after the execution of the programme.

Let see an example:

<?php

$x = 2;

$y = 4;

echo $x + $y;//Output: 6

?>

 

PHP is a loosely typed language, which means we don’t need to tell the php about the data type of the variable, it will automatically assigned the value to the variable according to it’s value. Since php is a loosely type language so we can add string to an integer without any error.

Scope

Php variables has three types of scopes:

1.    Local

2.    Global

3.    Static

Local Scope, when we define any variables within the function scope then the scope of that variable are only inside that particular function only. It can’t be accessed outside that function again.

Let’s see an example of local scope variable:

<?php

function myFunc() {

  $x = 10; // local scope

 echo "<p> $x is inside the local scope of this function</p>";

}

myFunc();

// using x outside the function will give us an error

echo "<p>We can’t use variable x here: $x</p>";

?>

 

Global Scope, if we directly declare a variable in php it is comes in the scope of global access, it’s means it can be accessed anywhere in the programme globally.

Let’s see an example of global scope:

<?php

$a = 5; // global scope

function myFunc() {

  // using a inside this function will generate an error

  echo "<p>Variable a inside function is: $a</p>";

}

myFunc();

echo "<p>Variable a outside function is: $a</p>";

?>

 

Static Scope, generally when we execute a function after the successful execution of the function the all variables declared in that function will be deleted, but if however you want to use that variable for further use, you can put a static keyword before that particular variable.

Let’s see an example of it:

<?php

function myTest() {

  static $x = 0;

 echo $x;

 $x++;

}

myTest();//Output: 0

myTest();//Output: 1

myTest();//Output: 2

?>

 

Global keyword, if we declare a variable inside a function and wants to access it outside the function we can do it by using global keyword before that variable. Let’s see an example of it:

<?php

$x = 5;

$y = 10;

function myFunc() {

  global $x, $y;

 $y = $x + $y;

}

myFunc();

echo $y; // outputs 15

?>

 

We can also access the global variable of php because php stores all of the global variables inside an array which is called GLOBALS array. We can access the array as $GLOBAL[index].


 

So, this all about variables and their uses in PHP. For understanding the concepts more clearly, you can pursue a complete course at PHP institute in Delhi. This will let you earn essential skills and confidence and you'll be able to become an expert web developer.

collect
0
avatar
Neha ADMEC
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more