5th Tutorial Creating and using Variables in PHP

Last Updated on

Variables in PHP


While any program load and runs in computer memory it require temporary memory locations called variables. Also when a use write a code or program he also need some values to save in temporary memory.

For this purpose we use variables in PHP and in every language. We can define the variables using $ sign and right after the variable name.

Remember PHP variables are case sensitive and should be used in proper case which you selected at variable defining time.







Assigning values to variable


When we create a variable. It don’t have any value itself so we have to assign a value to that variable as our requirements. To assign value to a variable we use logical operator Equal = also we can assign values to variable at defining point,





Here we assigned a value of 400 to a variable. We don’t need to set variable data type PHP will be get it automatically. if we want to store a string value then we will use to keep string in double quotes.

Or if we want to store numeric value then we can write as it is.


Storing numeric value



Storing string value

$VariableName=”Hello World”;


In example below we will use variables in a project to calculate 2 digits values.


Fist start your XAMPP server and create a folder in htdocs folder located in C drive in XAMPP folder.

Create there a new folder for this project and open that folder in text editor. I am using Sublime text as text editor.

READ  Introduction to Web development


And type the code as below:

Creating variables in php

After saving file run in browser and see the results Next tutorial Changing Data Types in PHP