Variables are storing containers for information. They help us to store values for example x = 3. Whenever we need to use value 3, we just call x and, in this case, x is the variable storing 3. Variables exist in any programming language but the way they are written differs from one language to another. We are going to see how variables are written in PHP and the different rulings but before we start, we need to create a file and store it in our php_projects folder we created in the introduction using any code editor of your choice. Let us name this file, variables.php. Files names should end with a .php extension and spaces in the file name should always be avoided, it is not a good practice to leave spaces in the file name.
Now that everything is set, lets start writing our first program in PHP using the code snippet below;
<?php
$hi = "Hello world!";
echo $hi;
?>
In any PHP program, we start with <?php and end it with ?>. All the code will go inside these two. In PHP, variables start with a $ followed by variable name for our case it is hi. Then this is followed with an equal sign which an assignment operator used to assign a value to a variable. Then the value which is Hello World! in our case is assigned to the variable $hi. We wrap the value in double quotes because it is made of string characters (check for operators post) though even single quotes work. Every statement must end with a semi-colon—; and that is why you see it at the end of every statement. It simply implies this is the end of the statement and without it, our program will return errors. Before we finish up with this line, lets analyze the rules governing writing of PHP variables
- They start with theÂ
$
 sign, followed by the name of the variable - A variable name must start with a letter or the underscore character e.g., $hello_world
- A variable name cannot contain a space. You can use an underscore or write it as a single word e.g., $hello_world, $helloWorld
- A variable name cannot start with a number e.g., $23alpha
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _)
- Variable names are case-sensitive (
$hi
 andÂ$Hi
 are two different variables) - A variable name cannot be a PHP reserved keyword.
In the next line, we see echo $hi; We need users to see the word Hello World! but to do this, we need some form of projection or output and that is where echo comes into the picture. It is used to output whatever value it has been given. In our case, we need to display Hello World! But since it is stored in a variable $hi, we just call this variable. Do not forget a semi-colon at the end.
To see this output, just go to localhost and look for the variables.php inside your php_projects folder.
You can see that our variables.php file is inside the folder php_projects. Click on this file and you will see the output Hello World! In case you get an error, do not worry, it is errors that build great programmers. It may be just a mistake done with the variable name or some php syntax. Just take a closer look at your code and compare with the one here. Happy coding 👩‍💻!
Whenever I want remind myself about something concerning php , I just come here , thanks for this info
Thank you, Brian.
It’s great to hear this Brian. Keep on learning.