We will learn about variables, constants and echo statements in detail in this PHP variables tutorial.
- Variables:
- Variable in any programming language is a name given to a memory location that holds a value.
- You can say that variables are containers for any type of values.
- There are some rules to write variable names in PHP.
- Rules for variable names:
- Variable names in PHP start with a dollar ($) sign followed by the variable name.
- Variable name can contain alphanumeric characters and underscore (_).
- Variable names must start with a letter or an underscore (_). (For eg: $abc, $x1, $_g, $abc_1 etc.)
- Variable names cannot start with a number.
- Variable names are case-sensitive. (for eg: $x and $X are treated as two different variables.)
- In PHP we don’t use any command to declare variables.
- A variable is created as soon as you assign a value to it.
- A variable takes a datatype according to the value assigned to it.
- Since we don’t have to specify datatypes for PHP variables, PHP is called as loosely typed language.
- Scope of variables:
- Variables can be declared anywhere in the program.
- Scope of a variable is a part of the program where the variable is accessible.
- PHP has three different variable scopes:
- Local
- Global
- static
- Local scope:
- A variable declared within a function has a local scope and can be accessed within a function only.
- A function is a small program performing a particular task which is called when required.
- Global scope:
- A variable declared outside a function has a global scope and can be accessed outside the function only.
- Actually global variables can be accessed anywhere using the global keyword.
- Static scope:
- A variable declared with static keyword is said to have static scope within the function.
- Normally when variables are executed, they lose their values or memory.
- But when a variable is declared as static, it doesn’t lose its value. It remains static within multiple function calls.
- Variable declaration:
- Variable is declared as follows:
$variable_name=value;
$x=5;
<?php $x=10; echo $x; ?>
- Write the following code in index.php file and test it by putting it in the newly created folder var_constant in htdocs folder of xampp folder.
- The code is as follows:
<?php $x=10; //global variable function myfun() { $y=20; //local variable echo "Value of var x inside myfun = ".$x; echo "<br>"; echo "Value of var y inside myfun = ".$y; echo "<br>"; } myfun(); echo "Value of var x outside myfun = ".$x; echo "<br>"; echo "Value of var y outside myfun = ".$y; ?>
echo "Value of var x inside myfun = ".$x; echo "Value of var y outside myfun = ".$y;
<?php $x=10; //global variable //echo $x; function myfun() { global $x; //accessing global variable inside function $y=20; //local variable echo "Value of var x inside myfun = ".$x; echo "<br>"; echo "Value of var y inside myfun = ".$y; echo "<br>"; } myfun(); echo "Value of var x outside myfun = ".$x; echo "<br>"; //echo "Value of var y outside myfun = ".$y; ?>
echo "Value of var y outside myfun = ".$y;
is commented in the program because it will give error since the local variables are not accessible outside the function.
echo "Value of var y outside myfun = ".$y;
- We discussed that static scope means the value of a variable is retained within multiple function calls.
- Let us try to demonstrate it.
- Write the following code in index.php file by commenting all the previous code:
<?php function static_eg() { $x=0; static $y=0; echo "non-static var x = $x static var y = $y <br>"; $x++; $y++; } static_eg(); static_eg(); static_eg(); static_eg(); ?>
- Constants are the variables whose values are not changed throughout the script.
- A valid constant variable do not have a $ sign before its name.
- It starts with a letter or an underscore (_).
- Constants have global scope in the whole script.
- Constants are useful in situations where same value is used in many places. For example: if we want to calculate an area and perimeter of a circle, we require the value of PI in both the cases. So we can have the value of PI defined as a constant and can use it effectively.
- If we want to create an array of names having length 10, we can define the length 10 as a constant which will be used anywhere required. But if for some reason we decided to increase the length to 20, we can just change the value 10 to 20 in the constant definition which will be replicated everywhere.
- Constants are declared using inbuilt define() function.
- It takes 3 parameters,
- Name of the constant
- Value of the constant
- Whether the constant should be case-insensitive. Default value is false.
- The third parameter of define() function is optional.
- The false value of third parameter of define() function denotes that the constant name is case-sensitive and true value indicates that the constant name is case-insensitive.
- Let us see how it is used in a program.
- First we will see the case-sensitive constant.
- Write the following code in index.php file:
<?php define("MESSAGE","Welcome to PHP!"); echo MESSAGE; ?>
<?php define("MESSAGE","Welcome to PHP!"); echo message; ?>
- Write the following code to define another constant:
<?php define("PI",3.14,true); echo PI; echo “<br>”; echo pi; ?>
- Echo statement is used to display something on the webpage or browser.
- Echo statement can display one or more strings.
- Echo statement can be used with or without parenthesis ().
- The word echo is a keyword. Keywords in PHP are not case sensitive and hence we can write it anyway. For eg: echo, Echo, EcHo etc.
- Echo statements can be used to display strings, values of variables etc.
- Write the following code to prove the above said sentence:
<?php echo "We are learning echo statement."; echo "<br>"; $name='Samuel'; echo $name; echo "<br>"; $age=20; echo $age; ?>
- Let us make a single sentence out of the values stored in the variables $name and $age.
- Write the following code below the above given code:
echo "<br><hr>"; echo "$name is $age years old. He is elegible to vote.";
- Two or more strings or values in variables can be concatenated as shown below:
- Write the following code:
<?php $fname='Harry'; $lname='Potter'; echo $fname.' '.$lname; ?>
Echo $fname.’ ’.$lname.’<br>’;
<?php echo '10'.'10'.'<br>'; $no1=29; $no2=56; echo $no1.$no2; ?>
- The final output can also be displayed by directly working out the calculation in echo statement.
- We can perform various mathematical operations in an echo statement.
- Let us see an example of obtaining an area of a circle of radius 5.
- Write the following code:
<?php define("PI",3.14,true); $r=5; echo PI*$r*$r; ?>
<?php define("PI",3.14,true); $r=5; echo "Area of circle with radius $r = ".(PI*$r*$r); ?>
Thus we finished studying variables, constants and echo statement in detail in this PHP Variables and Constants tutorial.
it very useful post thank you for sharing man.