i

PHP Tutorial

PHP Variables

Variables

A lot of times, we deal with all source of data and information in Php program and to maintain and keep track of all that data is very difficult. We use variables to hold that data in Php. Imagine that you plan to write a story and you have setup name of a character in your story. You thought to change the name of your character, but you have used the name in your story about 200 times. It will take you so much time and efforts to change the attribute everywhere in your story manually. But with the php variables, you can store that name of the character in a variable and just change the value of the variable to change the character name everywhere without any mistake and with minimal efforts.

Storing the Values

A certain piece of values or data that we want to keep track of, we save that information in variables to organize the data. Variable acts as a container for the piece of information. Syntax of the variable is $ name of the variable and to give the values to your variable add = and value. 

For e.g. 

$CharacterName = “Xoxo”;

Echo ” Once there was a man named $CharacterName”;

?>

The above script will print the output as Once there was a man named Xoxo. And when you change the variable name at one place, it will reflect the changes wherever it is used. 

Modifying the Values

You can also modify the value of your variable throughout the program. You can change the variable value any number of times, and from the place, you want to change. Before modification, the value will remain as before, and after modification, the new value will be reflected. Imagine you have written about one character, and now his story ended, and you want to change the character name, you can modify the character variable value.  

For e.g. 

$CharacterName = “Xoxo”;

Echo ” Once there was a man named $CharacterName.”;

Echo “$CharacterName died at the age of 80.”;

$CharacterName = “Saba”;

Echo “Then, his daughter named $CharacterName became the princess.”;

?> 

It will print the below lines on the webpage.

Once there was a man named Xoxo.

Xoxo died at the age of 80.

Then, his daughter named Saba became the princess.

So, the first part of the story will use the variable name as Xoxo and next part of the story will use the name, Saba. You can update the variable names throughout the php program and change the values. You can store text as well as numbers and other different types of data inside the variables. Let us discuss the datatypes in the next section.

Facts about PHP variables

Php variables can have a short name like x or a and can also have a descriptive name like color or ageofthecharacter. There are some rules of php variables as below.

  • A variable name starts with a dollar's ($) sign.

  • A variable name cannot start with a number.

  • A variable name should only start with a letter or underscore character.

  • A variable name can have characters, numbers and underscore only.

  • Variable names are case sensitive.

Php variables take the most recent assignment as their value which is assigned with the = operator. Declaration of the variable is optional. Php converts types from one to another automatically and automatically understands the datatype without specifying it.  

Scope of Variables

You can declare a variable anywhere in the php script but has three different scopes, i.e. local, global and static.

Local scope – When we use functions, a variable declared within a function will have a local scope and cannot be used outside a function.

Global scope – A variable that is declared outside a function cannot be used inside a function and can only be used outside a function. If you want to use a variable from a function, add the keyword global before the variable declaration.

Static Scope – Variables will be deleted when a function is called, but if you want a local variable to retain its value after the function is executed using a static keyword before the variable declaration.