Mathematical PHP
Jun 8
Today I’ll be talking about basic arithmetic functions and a little about PHP errors.
I’ll probably fill June with nothing but novice PHP tutorials just to get started, then carry on to more sophisticated PHP workings and probably a practical application or two. Well, let’s get started!
To recap:
Example 1: what we learned a few days ago
<?php
// A string
$var = 'value';
// A number
$some_other_var = 123;
/* Remember that
comments are ignored by
PHP completely. */
/* That dot is called the concatenation operator.
Concatenating strings just joins them together,
pretty much like linking chains. In this case, we're
putting the string '$var is ' and the value of $var,
which is 'value', together, to get '$var is value'. */
echo '$var is ' . $var;
?>
OK, it seems I just taught a little something up there. Anyway, let’s begin first talking about basic arithmetic operators.
Example 2: addition, subtraction, multiplication and division
<?php
echo 1 + 2; // 3
echo 6 - 4; // 2
echo 9 * 1; // 9
echo 6 / 3; // 2
?>
Most of us would just key in the small letter X to represent multiplication, and the oblique slash (/) to represent division. The asterisk (*) represents multiplication here, and it’s the same for division (a slash).
Let’s try a bunch of operators and throw in a couple parentheses too…
Example 3.1: lots of math
<?php
$result = (2 * 63 / (7 * 3) - 147) * -20;
echo $result; // 2820
?>
Recall your BODMAS ordering of arithmetic operators. Brackets, orders, division, multiplication, addition and subtraction.
Example 3.2: how the above works out to 2820
(2 * 63 / (7 * 3) - 147) * -20 = (2 * 63 / 21 - 147) * -20 = (126 / 21 - 147) * -20 = (6 - 147) * -20 = -141 * -20 = 141 * 20 = 2820
You can negate a variable if it’s a number too:
Example 4: negating a variable
<?php
$n = 20;
$n = -$n;
echo $n; // -20
?>
We can assign every number we want to variables.
Example 5: variables
<?php
// ¡No hablo español!
$uno = 1;
$dos = 2;
$tres = 3;
// I speak English.
$one = $uno;
$two = $dos;
$three = $tres;
$twenty_six_hundred_and_five = 2605;
$million = 1000000;
$sum = $one + $two + $three + $twenty_six_hundred_and_five + $million;
/* We get 1002611. print() is an alternative to echo() but
returns 1. I'll talk about return values next time. */
print $sum;
?>
Do you remember that we can’t divide by zero? So does PHP.
Example 6: division by zero
<?php
$result = 80 / 0;
?>
What do we get? Some noise from PHP.
Warning: division by zero in D:\NOVALISTIC\3.0\example.php on line 3
When PHP throws a warning, it continues processing anyway, but you have to be careful because warnings alert you of unexpected things going on in your code. It may lead to other consequences…
Let’s look at some other error types.
Notice: Undefined index in D:\NOVALISTIC\3.0\example.php on line 141
Parse error: unexpected ‘,’ in D:\NOVALISTIC\3.0\example.php on line 568
Fatal error: call to undefined function some_undefined_function() in D:\NOVALISTIC\3.0\example.php on line 24
Notices shouldn’t pose any problems to you, your code or anyone else, but hardcore PHP standards-freaks would look down on applications full of notices. There are millions of developers who don’t care about it and PHP, by default, will not output any notices, because they aren’t too big deals.
Parse errors are just what they are: parse errors. A parse error occurs most likely because you’ve got a typo in your code, like missing parentheses or brackets, or semicolons. PHP will immediately stop work upon encountering a parse error. Pretty much like grammatical errors in your essays.
A fatal error is what happens when something goes really wrong. When you call a function that doesn’t exist, a file that you require() doesn’t exist (but you HAVE to have it - I’ll explain include() and require() some other day), or something else really bad goes on while PHP is doing its thing. PHP will immediately stop work upon encountering a fatal error.
So you’ve gotta be a little careful with coding.
Comments closed
Sorry, but commenting for this post has been closed.