If you have ever looked through any programming language – or still remember those algebra classes when you learned what 3+x=5 means then you already know what a variable does. A variable stands for something else, or in PHP it stores something inside of it so that you can recall it later on in your scripting. So let’s take a look at creating variables and storing information in them through PHP.


Let’s start by taking a look at how we set up variables in the first place.

Above we have set three variables, $name, $numberOne and $numberTwo. Then we added the two numbers together into a fourth variable: $numbersTotal. Now there’s a few things to factor in here:

  • A variable always starts with the $ sign, followed by the name of the variable
    • A variable name must start with a letter or the underscore character
    • A variable name cannot start with a number
    • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
    • Variable names are case sensitive ($y and $Y are two different variables)
  • String based variables must be enclosed in quotes
  • Numerical variables do not require to be enclosed in quotes

Further on we could recall these variables using our previously discussed echo(); function. Let’s take a closer look:

This will output the following:

My name is: Michael B
The sum of 5 and 10 is 15

The \\n tells PHP that a linebreak should be inserted there. Without the /n the “The sum of …” echo would have been displayed directly following the previous one – on the same exact line.

This covers the basics of PHP Variables. Next we discuss variable scope, global, and static variables.

Published by Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Join the Conversation

4 Comments

Leave a comment

Your email address will not be published. Required fields are marked *