Web Designing Tutorials

How To Use Different PHP Variables

Pinterest LinkedIn Tumblr
Before starting to go PHP variables, let’s know what the variable is. A variable is a memory location along with providing the name. Since, the variable contains data that may be manipulated throughout the execution of the program. We can also say that, a variable is a symbol that can share different values at different times.

As the variables used in different programming languages, we can also use different types of variables in PHP. The variables used in PHP are classified based on the scope of the variables. Before knowing about the types of variables, let’s discuss on how to declare and assign values to the PHP variables.

Declaration And Assignment of PHP Variables

We can declare a PHP variable with just writing variable name that begins with $ sign. PHP variables should not have to be explicitly declared. Rather the variables can be declared and assigned simultaneously.

Every PHP variables should always begin with either a letter or an underscore. So, the PHP variable can consist of letters, underscores, numbers or other ASCII characters ranging from 127 through 255. Following are the valid examples of PHP variables.

$marks

$number

$_my_variable

Since, the variables used in PHP are case sensitive. Following variables are different.

The Variables $Total and $total are different, Whereas the variables $tOTAL and $Total are also different.

I have mentioned earlier, you can assign the PHP variable at the time of variable declaration. Assignment of the variable is simply assigning the expression to the variable. Here are some examples of assignment of the PHP variables.

$marks=65;

$number=60+”15″;

$my_variable=”PHP”;

PHP also allows you to assign variables by reference. So you can create a variable that refers to the same content as another variable does. Therefore, any changes to one variable reflects among other variables referencing same content. Following is an example of assigning values of the variable by reference.

$string1=”Good Morning”;

$string2=&$string1; // Assigning the value of $sting1 to $string2 by reference

$sting2=”Good Afternoon”;  // Both $string1 and $string2 assigns the value “Good Afternoon”

Types of PHP Variables

Although, you can declare variables anywhere within your PHP coding.  We can classify the PHP variables based on the location of the variable where we declared. The location of the variable also influences the variables we want to access called the scope of the variable. Following are the different types of variables we can classify based on the scope of the variable.

Local Variables

If we declare the variable within the function called the local variable. Since local variables can be referenced only within the function. So, you are unable to access, assign and alter the value of the local variable outside the function. All the variables declared inside function are destroyed.

If any variable is useful only within function, we should declare it as a local variable. Unnecessary declaration of global variable may produce unexpected result due to accidental modification. Following is an example illustrating the usage of local variable in PHP.

<?php

$age=42;    // variable declared outside function

function displayage(){

$age=31;  // local variable declared inside function

echo “Your age was “.$age;

}

displayage();

echo “<br/>”;

echo “Your age was “.$age;

?>

Output:

Your age was 31
Your age was 42

In the above example, you have seen the output of different values of the variable $age. It was happened due to the local variable declared inside the function displayage(). Hence, the local variable do not bring any value from the variable declared outside the function.

Function Parameters

While creating a function that accepts arguments, you must declare those arguments at the header of function. The variables which are declared after the function name are used as function arguments are called function parameters. In other words, local variable becomes function parameter, if that variable is used as function argument.

Following is an example that illustrate the uses of function parameters in PHP.

<?php

function square($num) {

$num=$num*$num;

return $num;

}

echo “The square of 9 is “.square(9);

?>

Output:

The square of 9 is 81

Global Variables

Unlikely the local variables, you can access the global variable anywhere in the program. Since, the global variables declared outside all the functions and at the top of the program. It can be accessible within the function but it must be explicitly declared to make variable global. Before using global variable inside the function should declare placing the keyword global in front of the variable.

Following example shows how to declare and use the global variable within function.

<?php

$mynum=25;

function double(){

global $mynum;

$mynum=2*$mynum;

echo “Double of the number is $mynum”;

}

double();

?>

Output:

Double of the number is 50

While running the code above, the value of the variable $mynum would be 50. However, if you do not write the expression global $mynum, the variable $mynum would be local variable. So, declaration of the local variable would automatically assign value of the variable zero.

There is an another method for declaring global variable, which is using PHP’s $GLOBALS array. Following example shows the usages of $GLOBALS array to declare the variable $mynum global.

<?php

$mynum=25;

function double(){

$mynum=2*$GLOBALS[“mynum”];

echo “Double of the number is $mynum”;

}

double();

?>

Output:

Double of the number is 50

Static Variables

You have already know that the variables declared as function parameter or local variable are destroyed on the function exit. While the static variable still holds the value if the function called again. So, this type of variable mostly useful for recursive functions in order to loop the function for certain conditions.

You can declare static variables with simply placing static keyword in front of the variable. Following is an example that shows how static variable performs unlikely of using local variables.

<?php

function make_square() {

static $num=2;

$num=$num*$num;

echo $num.”<br/>”;

}

make_square();
make_square();

?>

Output:

4

16

Read Next: What Are Different Types of PHP Operators

Article Sponsor: online casino New Zealand

Please follow and like us:
RSS
Follow by Email
X (Twitter)
Visit Us
Follow Me
YouTube
YouTube
LinkedIn
linkedIn | How To Use Different PHP Variables
Share

Author

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 8 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Comments are closed.

RSS
Follow by Email
X (Twitter)
Visit Us
Follow Me
YouTube
YouTube
LinkedIn
Share