PHP – variable declaration

PHP is an easy server side scripting language. To accomplish various functionality and tasks, we require variables. PHP has its own of declaring variables. Lets see in detail.

What are variables in PHP?

Variables are developer defined words which holds certain information. (Who are developers? The one who is coding. What are information? Integer value, String value, Array Value etc.)

Example:

Suppose, I want to print the addition value of 2 and 3. The syntax would be as below:

<?php echo 2+3; ?>

Now place the code in index file, save it and run it. You would get 6 on the webpage.

In real project scenario, we may get values from different source and then we need to add it. (For now, forget how values are collected from other source, but focus now, we got two values and we need to add and print the value)

Our first job would be to capture those values. In simple language, we need to put in some kind of container. This container is going to hold the value.

PHP Data Variable Declaration

Look into the picture. Here we are receiving two values 5 and 6 from outer source. To perform addition operation on this two value, first we need to capture in individual container. This container is known as variable in PHP / Programming World. Once we capture in a container then we can easily perform required task. (Here our task is addition). In coding, capturing value in a container is known as declaring a variable and assigning value to it.

How do we declare variable in PHP?

In other programming language such as Java, C, C++, C#, we declare a variable using its datatype keyword, variable name and value (E.g int num=5;)

But in PHP, we don’t require keyword to define a value type. We need to define a variable name only, and server engine going to understand the type of the variable from its assigned value.

How do we define variable in php?

To define a variable in php, we use dollar sign ($) followed with a name.

Example:

$num=5;

See, $num is variable and a value of 5 is assigned to it. From the assigned value, PHP Engine going to understand that $num is an Integer value. We don’t require separate declaration for it.

Note: after dollar sign, immediate variable name should appear. Otherwise, it will raise an error.

Note: In variable name, no space and special characters are allowed. Numbers can be used. Underscore can also be used.

Few Examples:

$first_num=5;

$firstNum=5;

$FirstNum=5;

In the above example, what we are doing?

We are declaring a variable and assigning 5 value to it. Here $first_num, $firstNum, $FirstNum are known as variable names. And 5 is the value we are assigning.

In the picture representation, these variable names are similar to jars. Variable names hold value or data.

Example:

Lets assign 5 and 6 to two variables and print addition value.

<?php $num1=5; ?>

<?php $num2=6; ?>

<?php echo $num1+$num2; ?>

Copy the code to your index file, save and run it. You would get 6 on the webpage.

Note: The above code can also written following way:

<?php

$num1=5;

$num2=6;

echo $num1+$num2;

?>

Copy the code to your index file. Save it. And run it. You would get same result 6 on the webpage.

In similar way we can also declare string variable.

Example:

<?php $MyName=”Srikant Kumar”;

echo $MyName;

?>

Try the above code and run the index file.

Remember:

String value is always kept in between single quote or double quotes.

So this way, we declare variable in PHP.