Ads

Tuesday, November 15, 2016

PHP - Use of Variables

Today, we will learn the use of variables in php scripting.Variables are also called memory locations where we store the input/processed data.In php, We use a $ (dollar) sign for variables and we can use it anywhere in the script while execution.To set a value of a variable, we use = (equal) sign.The value of variables in any program cannot remain constant.It means that the value of variable can be changed in the program while executing it.Here is sample php script where we'll use variables to print data on the screen.












Simple PHP Script with Variables:


<?php

  $Var1 = "My First program...";

echo $Var1;

?>
Output:

My First program...

Please note that all the strings must be written between double quotations " " otherwise you will get error message while executing the script.To write a number or mathematical operations, we can use them without double quotations.Here is an other php script where we'll define a variable and set its value as a number

 Numbers in Variable:


<?php

  $Var1 = 100;

echo $Var1;

?>
Output:

100
Here is another example of getting sum of numbers by defining a variable

<?php

  $Sum = 786 + 234;

echo $Sum;

?>

Output:

1020

In next article, we'll learn the use of Constants in PHP

    Choose :
  • OR
  • To comment
No comments:
Write comments