Lesson 3 – PHP Variable Types

The basic purpose of PHP variables is to store the information which you can use at some other point. There are certain conditions that you have to meet while creating variables.

  • All the variables in PHP must start with a dollar ($) sign.
  • The most recent assignment of a variable is its actual value.
  • We use the “equals to” operator to assign the value to a variable.
  • The variable name must start with a letter or underscore character. It cannot have characters like + , – , % , ( , ) . & , etc.

Now, let us create our first-ever variable in PHP.

<?php 
$number = 12345;
?>

The variable in the above example has an integer as value. Integers in PHP are whole numbers such as 12345. They do not have a decimal point. Both negative and positive whole numbers are classified as integers.

Similarly, we can also create a variable with a string as a value. A string is simply a sequence of characters. “PHP is an important programming language to learn” is a string. We always enclose strings in double or single quotes. The following is an example of a string variable.

<?php 
$string = "This is a string variable";
?>

If you want to print these variables, you will simply use the echo function.

<?php 
$number = 12345;
$string = "This is a string variable";

echo $number;
echo $string;
?>

Now, the Apache will print the value of these variables on your screen.

Keep in mind that PHP is case sensitive which means CAPITAL and capital are totally different variables.

<?php 
$CAPITAL = "This is a totally different variable";
$capital = "capital is not the same as CAPITAL in PHP";
?>

You can also concatenate two or more variables to put data together in PHP. The process of concatenating variables is quite simple. Take a look at the following example.

<?php 
$number = 12345;
$string = "This is a string variable";

Echo $number . " " . $string;
?>

To concatenate variables, you put a dot (.) between two variables. The double quotes in our example put some extra space between the two variables as shown in the following image.

In the next lecture, we will study simple mathematical operators or how to do mathematics in PHP.