Home > Tutorials > PHP Tutorials > PHP Variables – Understanding The Basics

PHP Variables – Understanding The Basics

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.

$name = "Michael B";
$numberOne = 5;
$numberTwo = 10;
$numbersTotal = $numberOne+$numberTwo;

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:

echo "My name is: $name \\n";
echo "The sum of $numberOne and $numberTwo is $numbersTotal";

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.

About 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.

Check Also

Installing Gnome Software Center on a Chromebook

Linux is pretty much all the rage these days when it comes to Chromebooks – ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.