Variables in PHP, just like any programming language, are used for storing values, such as text strings, numbers, or an array of values. Once you have declared, a variable, it can be used over and over again throughout your script.
Variables in PHP must be declared with a ‘$’ symbol. So a variable named test would be declared as $test. The ‘$’ symbol is also needed every time the variable is used after it is declared.
A simple variable declaration should be done as followed :
Now let’s make one variable with a string, and another with a number value :
- Note – A string, like the echo statement, can use either single or double quotes, but it must end with whichever it starts with.
There is a lot of freedom when using variables in PHP. First of all you do not have to declare the variable before it is used. The first time you use the variable it will be declared. You also do not have to declare the data type (string, integer, float) of the variable. Once you use the variable, PHP automatically changes the data type to whatever value is stored in the variable.
You can also change the data type of the variable multiple times in your script. Example :
- Note – This can be done as many times during your script as you’d like.
With all the freedom that comes with PHP variables, there are some rules when it comes to naming them :
- PHP variable names must start with a letter or an underscore.
- PHP variable names can consist of only alpha-numeric characters and underscores: a-z, A-Z, 0-9, and _
- A PHP variable name cannot contain spaces. If a variable is more than one word use an underscore in between them such as ‘$my_variable’ or use capitalizations like ‘$myVariable’.
Now let’s make a simple PHP script that can show us the effects of using variables :
So what’s going on here? First we set a variable to equal the string ‘Hello World!’, then another variable to equal the number ’23′. After that we used the echo command to print on screen what was stored in the ‘$hello’ variable. The second echo statement prints a string followed by the ‘$fav_num’ variable.
- Note – In the second echo statement, to connect the string to include the ‘$fav_num’ variable, we use a period in between the two. This is known as concatenation, or to concatenate.
When you open your newly made script in a browser it should look something like this :
That pretty much covers the basics of PHP variables. Try to write some scripts with what you’ve learned to get used to using variables. A good thing to try also, is naming the variables something that isn’t recommended, just to see what the error looks like that PHP would likely spit out.
As usual if you’ve got any questions, just comment below.
Before we doing any coding, let’s talk about what PHP is. PHP (Hypertext Preprocessor) is a widely used scripting language that is designed for web development and can be embedded directly into HTML.
One of the strongest features of PHP is its ability to connect to a wide range of databases. Those features will be touched on in later, more advanced tutorials.
This tutorial and any further PHP tutorials will assume that you will be using a server that supports PHP, and that you know HTML. If you don’t know HTML then I strongly recommend not reading this tutorial until you’ve learned it.
Now let’s begin writing your first PHP script.
- Note – There are several programs you can use for writing PHP, but if you don’t have a specific program such as Adobe Dreamweaver or Panic Coda, you can simply use your computer’s most basic text editor.
First off, create a new file and save it as helloworld.php. Yes the infamous ‘Hello World’. If you don’t know what I’m talking about and plan on learning other programming languages later, then get used to seeing ‘Hello World’ as your first tutorial.
I’ll start by showing you the code, then explaining it piece by piece after you’ve looked through it. I’ve found that’s the best way to work with stuff like this.
Yup, that’s it.
We start by creating a blank html document with all of the standard tags. Only one thing, there’s something new in the body tag.
Whenever you write PHP code, you have to start with “<?php” and end it with “?>”. This lets the server know where the PHP code is within the HTML file.
- Note – “<?php” tags can be placed anywhere in the HTML document.
The only line within the PHP tag is “echo ‘Hello World’;”. The ‘echo’ command tells PHP to write on the screen whatever text you put within the following quotes.
- Note – You can use either single quotes or double quotes for ‘echo’, but whichever one you open with, you must close with as well.
- Note – You can also put HTML code within the quotes following ‘echo’. This will become very helpful later.
The last thing in the line is the semi-colon, which has to be placed at the end of every PHP line, to let PHP know that the command is done, and to move on to the next line. Without the semi-colon, the page would result in an error.
Here’s what your page should look like after opening it in your browser :
That’s really all there is too it. Thanks for taking the time to read this tutorial and I hope you enjoyed writing your very first PHP script. If you have any questions, please comment below.