I haven't done many PHP Tutorials recently so I decided I'd be writing a couple of PHP tutorials over the new few days, pretty basic stuff nothing complicated. In this tutorial were going to be learning about variables and numbers in PHP, im going to show you how they work and how you can use them, you'll also learn how to create your own variables and how to use pre-defined variables. You'll also see how they work and how you can apply them to future scripts that you may make in the future!
First off before we continue, you will need some form of test server that can handle PHP so you can display the examples that you will see in this tutorial. Don't be alarmed this doesn't mean you have to go out a buy a server, if your on a Windows Machine you can follow my Localhost tutorial series which will be more than enough to set you up with a localhost development area. You can view all 4 parts of the tutorial here
Once you get a localhost development setup continue on with the tutorial. But before you do read below, this will be a very basic tutorial using variables and numbers in PHP so don't expect anything amazing.
What is a variable?
There are so many variables in PHP that you couldn't list them all because a variable can be anything you like lets get on with some code:
<?php $first_number = 10; ?>
The variable here is first_number. A variable is signified by a $ sign, why? Simply because thats how variables roll (Pardon the gangster talk) with that being said the value of this variable is 10. Seems the tutorial is titled variables and numbers we will be involving them more as we go along.
Adding functions to the variable
On it's own that variable will do nothing we need to add a function to the variable which will make it worth while a basic function we can use is the echo function. We could for example create a small script that will display the value of the variable first_number:
<?php $first_number = 10; echo ($first_number); ?>
The echo function is mainly used to display variable value's and can also be used to display basic text combind with variable values. You can also use the print function which does a similar job, it's up to, I've just never used the print function always echo. If you wanted to use the print function you would simply use this version instead:
<?php $first_number = 10; print ($first_number); ?>
Use whichever function you prefer. There is a difference between echo and print, but for this tutorial it's not worth explaining (especially if your new to PHP) The above scripts will simply display the value 10 if you viewed the script. Because all that the scripts being told to do is display whatever value is set on the variable first_number.
Extending the echo function
So at the moment all that happens is the value 10 displays when you run the script. We can expand on this by using the variable direct text:
<?php $first_number = 10; $direct_text ='My variable has the value of '; echo($direct_text . $first_number); ?>
We now have added in the variable direct text. This simply can extend the echo function. Instead of displaying the number 10 using this script will now display My Variable has the value of 10. But when using the direct text you always have to remember to add it into the echo statement along with a . at the end of the direct text variable.
Calculations in PHP
I hate Maths but you can use PHP to do calculations of sums. This would be useful for scripts that use some form of discount system or shopping cart etc. It requires a bit more knowledge but we can build off the current script we've been writing to fully understand how to calculate.
First up Addition:
To add numbers in PHP you use the + sign (No difference there then) were going to introduce a new variable $sum_total into the code but first we need to have a second number to actually add together, so I created a second variable called second_number and gave it a value of 20. We can now use the sum_total to add these two numbers like so:
<?php $first_number = 10; $second_number = 20; $sum_total = $first_number + $second_number; $direct_text ='10 + 20 =' ; echo ($direct_text . $sum_total); ?>
Really we've only changed a bit of the script. A introduction of a new variablew second_number and instead of echoing the variable first_number we simply change it to $sum_total that way it will display the calculation of 10 + 20.
Next up Subtraction:
To subtract something in PHP much like reality you use the - sign, and seems we've already introduced the sum_total variable all we need to do is change the sum and the first_number and second_number variable, but lets make it more interesting lets introduce a third number by creating a new variable third_number here's some subtraction in PHP:
<?php $first_number = 100; $second_number = 20; $third_number = 10; $sum_total = $first_number - $second_number - $third_number; $direct_text ='100 - 20 - 10 =' ; echo ($direct_text . $sum_total); ?>
Introducting a third number doesn't really do much, you could have about 10 different variables e.g. fourth_number, fifth_number etc, but thats subtraction.
Finally, Multiplication and Division:
Why have I created a section with these two together, well you'll find out. Mulpication isn't represented as x in PHP instead it recongises * as a multiply sign. Here's a example of it being used:
<?php $first_number = 10; $second_number = 2; $third_number = 3; $sum_total = $third_number * $second_number * $first_number; $direct_text ='3 x 2 x 10 = '; echo ($direct_text . $sum_total); ?>
Hopefully your getting the hang of using sums in PHP, but wait! Im about to confuse you now because what if I was to mix it up a bit and use both multiplication and addition in the script? You can do this yes but you have to be aware of something and I'll show you what in this script:
<?php $first_number = 10; $second_number = 2; $third_number = 3; $sum_total = $third_number * $second_number + $first_number; $direct_text='3 x 2 + 10 (Without Bodmass) = '; echo ($direct_text . $sum_total); ?>
In this script we have combined multiplication and addition but notice the brackets in the sum total variable the second_number and first_number variable have brackets around them, what do they mean? Well do you remember something called BODMAS in Maths? No well first of what does this Bodmas stand for:
Brackets
Order
Division
Multiplication
Addition
Subtraction
Oh no school work in a tutorial! Don't worry we won't hang on it for much longer. But basically Bodmas is the order of which sums are calculated, for example if you used a calculator to work out this sum 3 x 2 + 10 then it would equal 16 because the calculator will work out the multiplication first according to Bodmas but we can work around this by using brackets in the sum_total to clear up what needs to be worked out in order to avoid strange answers. We simply add brackets to the variables second_number and first_number:
<?php $first_number = 10; $second_number = 2; $third_number = 3; $sum_total = $third_number * ($second_number + $first_number); $direct_text='3 x 2 + 10 (Using Bodmass) = '; echo ($direct_text . $sum_total); ?>
Okay for real now finally Division:
You can't make a division sign on the keyboard so the way to represent this in PHP is by using the / sign here's a script combines division and subtraction again with the use of brackets and bodmass:
<?php $first_number = 10; $second_number = 20; $third_number = 100; $sum_total = ($third_number - $second_number) / $first_number; $direct_text ='100 - 20 / 10 = '; echo ($direct_text . $sum_total); ?>
Without the brackets the Division would be calculated first but this would give us a very wrong answer and hence the brackets are required to make sure the subtraction part if calculated first.
And thats really the basics of Variables and numbers in PHP. You can play around with some sums and use brackets to see what answers you get with and without them, so play around and see what you can get!










Abraham
June 28th
Great post, looking foward for more PHP tuts!
James
June 28th
Thanks Abraham, Next one will be hitting the frontpage in the next few days.
Fire G
July 1st
Why do you keep writing all your echos with brackets? Echo isn't a function. You're supposed to use quotes everywhere that you used a bracket.
James
July 1st
Because when your echoing a variable you don't need to use quote marks. For text or whatever you do.