Variables


 

Variables

Variable is an identifier used for storing the values like text strings, numbers or arrays which is stored in the system's memory and can be retrieved any time during the program.

Variable is an identifier used for storing the values like text strings, numbers or arrays which is stored in the system's memory and can be retrieved any time during the program.

Variable is an identifier used for storing the values like text strings, numbers or arrays which is stored in the system’s memory and can be retrieved any time during the program. After declaring the value of variable, it can be used unlimited time during the program.

All variables in PHP begins with a $ symbol. Here is an example of declaring the correct way of variable:

$var_name = value;

Remember, always use the $ symbol at the beginning of the variable, otherwise it will not work. For more clarity, lets see an example of Hello World:

 <?php
$txt="Hello World!";
$x=16;
?>

The output will be like - Hello World.

In terms of value declaration, PHP is an exceptional language that does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value. For example:

1. <?php
2. $string = “Hello Users”;
3. print (“Welcome to Roseindia, I want to greet $string”);
4. print (“<p>”);
5. $string = “Bye Bye!”;
6. print (“OK meet you soon, $string”);
7. ?>

3.4.1 Rules for Declaring Variables

  • A variable name must start with a letter or an underscore “_”
  • A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
  • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($rose_string), or with capitalization ($roseString)

Variable names are case sensitive and can be used carefully. Besides, string and numbers, variables can also be in some other things like objects, arrays, Booleans, etc.

Ads