PHP Tutorials 4: How to Define and Print Variables

PHP Tutorials 4: How to Define and Print Variables

Variables are one of the needed parts in almost all programming languages. First I am going to explain what is a variable and how it works. Suppose that you have an empty bowl, and you can put anything you want in it. Once you would put some water, then some fruits and so on… In this example your bowl is as a variable that can carry different thing in every instant.  PHP variable are the same!

As we mentioned before variables are in different programing languages and, are of important parts of a project. You would need variables in all projects so try to learn this section deeply. Other languages are keeping and defining different data types, but PHP is vice-versa. There is no need to mention data types in PHP. So this is one of the positive points in PHP, therefore you can define a variable without telling whether it is of which data type.

There are some rules for defining and using variables.

  • For defining a variable first put “$” sign and right after that write the variable name. ($variable)
  • Keep in mind that PHP is CASE SENSITIVE and there is difference between ($var) and ($Var) and they are two variables in PHP.
  • Variable names cannot contain special characters except Underscore (_).
  • Identifiers or Variable names must not be started with numbers. (values can be everything)
  • Values can be defined to variable by putting an (=) sign.

Let’s work practically:

Create a .php file in the www folder as we learned in the first topic (Creating first PHP Project) and put the following code inside that:

As you see, it is possible to put every type of data in a variable in PHP. You know there are different types of variables if you’ve worked with any other programming language.

  • String variables
  • Integer variables
  • Floating Point variables
  • Boolean Variables

This four types of variables are almost in all programming languages. A variable will obtain the same data type, as its value is obtaining, so whatever data you assign to a variable, variable will be of that data type in PHP and there is no need to mention the data type is self like other programming languages.

  • You have to put string values inside double quotes or single while assigning them to a variable.

Printing PHP Variable:

We learned how to print different data in previous tutorials (How to Print in PHP), but let’s talk clearly about printing variables. For printing a value assigned to a variable you can use the echo command in PHP.  Take the previous .php file you created, remove the written codes, place or write the below codes and save the file. Start WampServer, Open the file you created the way we taught before (Creating first PHP project).

Fine, Your browser might look as below:

php variable

Now this is the end of this tutorial, next we will have more about PHP. Stay tuned with codejow

Raza Bayani

Raza Bayani studies BCE (Bachelor in Computer Engineering) in Poly-technical University of Kabul. He is the CEO of CODEJOW group, a web designer, coder and recently a blogger. Raza thinks, he can help others and also boost-up his own programming skills being on

Related Posts