Variables in PHP – Free PHP Tutorials

What is a variable and what is it used for?

What are variables used for? This question must click a programmers mind. So here is your answer.

Basically, we need some container where we can contain i.e hold something, hence the variable acts as a container to store the values.

These values can be of any type such as integers, decimal values(often referred to as float in most of the programming languages), strings(array of characters) and many more. We have the liberty to use the same variable again and again in our script.

In PHP, the following syntax is followed for using a variable:

$variable_name=value;

A variable must always be preceded by the ‘$’ symbol. Most of the beginners make this rookie mistake of not using the $ symbol.

See the following code snippet:

<?php

$var=”Welcome to Padhle.com!”;

?>

You can see in the above code snippet that we have assigned a value “Welcome to padhle.com” to the variable “var”.


Why we don’t require the declaration of a variable in PHP ?

Now you must be wondering why we have not defined the datatype for the above variable, so how can we allocate a string value to the variable. The answer to the question is that, since PHP is a loosely/freely typed language i.e. the variable used is not required to be declared. It’s not like that we can’t declare the variables but there is no need to do that since it is being handled internally.

PHP is also quite good at converting the datatype of the variable to another efficiently whenever required.

In the case of PHP, the datatype for a variable is automatically defined once the values are assigned to it.

So this reduces the burden of declaring a variable from the programmer’s shoulder!


Rules for Naming

Now we must follow some norms and standards that must be used for naming a variable in PHP. The most important points that must be kept in mind while naming a variable in PHP are as follows:

  • A variable must start with alphabets or underscores i.e. (a-z/ _) only
  • A variable name should not have any white spaces, instead, use underscores (_).
  • After the first letter of the variable name, other letters can be numeric also.
  • Variables in PHP are case sensitive i.e. variable name “name” and “NAME” are different from one another.

See the following code snippet. Here we have covered most of the cases of different combinations of variable names.

<? php

$Variable=1;

$var=”123”;

$_name=”aditya”;

$1age=19;

?>

Difference between $ and $$ variables in PHP ?

The single $ variables are the normal conventional type variables. But double $$ variables are references to the single $variables i.e. it stores the value of other variables inside it. See the following code snippet for example:

<?php

$name=”aditya”;

$$name=”adi”;

echo “”.$name;

echo “”.$$name;

echo “”.$aditya;

?>

The output will be as follows:

aditya

adi

adi

The output clearly shows the implementation of the $ and $$ variables inside a program.

For any further queries and doubts, leave your comments in the section given below.

Leave a Comment

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