A quick overview of Variables in PHP

As we all know every language whether it be a scripting language or a programming language has variables in it. php which is a server-side HTML embedded scripting language also has variables which help it to store values anytime it wants it to. 

The variables in php are very easy to use and they need not be declared to specific type like int,float,String etc, we just have to assign a value. This is because they automatically convert to the data-type we require. So there's no need to declare them in a different way each time, or create a value for them each time.

The variables has to start with a $ sign and then a name for the variable. The name cannot start with a number, it can either start with an underscore (_) or a letter. No other special characters are allowed except underscores, letters and numbers, as long as it doesn't start with a number.  

The line termination is done with the help of of a semi-colon (;).

Lets take a look at an Example and understand it better :


$topic = "php tutorial on Variables";
$number = 1;

echo "Code 2 Learn Post on $topic part $number";

In the above example we have taken 2 variable namely topic and number. the value in topic is in " " because we need to make it a String type and the value in number is not in " " because we wanted number to be integer. We can also put 1.5 or any other value and the datatype will accordingly change itself.

The echo" " is used to print the output onto the screen of the user.

And also the thing with variable is the they are easy to concatenate. In fact, probably, concatenation is the wrong word – they're very easy to include inside your string. Here we have done just that, i.e. we have included the value of $topic and  $number.

The output of the above code would be :

Try the below code and check the OUTPUT :

This is all you need to know in variables. There are many types of variable like boolean, float etc, but you would have to declare them in the same way as we did above.

So practice this and you can come back and learn some more advanced functionality later on, when I'm going through some other projects.



  1. There is no decimal type in PHP. There are exactly 8 types: