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:
// string variable
$var1 = "codejow";
// Integer variable
$var2 = 123;
// floating point variable
$var3 = 1.2222;
// Boolean variable
$var4 = true;
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).
//This is a description!
$var = "value of the first variable.";
// Below is a variable with number values!
$newVar = 123;
// Line below indicates boolean variable.
$anotherVar = true;
// Below code will print the string along eachother on the browser.
echo "The value of first variable is: $var";
echo "Second variable value : $newVar and third variable valueis : $anotherVar ";
Fine, Your browser might look as below:
Now this is the end of this tutorial, next we will have more about PHP. Stay tuned with codejow