Created
August 22, 2012 01:58
-
-
Save recck/3421441 to your computer and use it in GitHub Desktop.
Week 1 - Day 2 - PHP Syntax, Variables and Mathematical Operations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Mathematical Operations | |
* Addition, +, 5+2 = 7 | |
* Subtraction, -, 5-2 = 3 | |
* Multiplication, *, 5*2 = 10 | |
* Division, /, 5/2 = 2.5 | |
* Modulus, %, 5%2 = 1 | |
* | |
* Short Hand Operations | |
* $variable++; Adds 1 to the variable | |
* $variable--; Subtracts 1 from the variable | |
* $variable += # Adds # to the variable | |
* $variable -= # Subtracts # from the variable | |
* $variable *= # Multiplies the variable by # | |
* $variable /= # Divides the variable by # | |
* $variable %= # Calculates the modulus of variable % # | |
*/ | |
$firstVariable = 'Hello world'; # this is a variable containing a string | |
echo $firstVariable; | |
$secondVariable = 5; // this is a variable containing a number | |
$thirdVariable = 1.5; /** this is a variable containing a floating point **/ | |
/** | |
* now let's add some variables | |
* together! | |
*/ | |
$fourthVariable = $secondVariable + $thirdVariable; | |
echo $fourthVariable; | |
$fourthVariable += 7; // equivalent to $fourthVariable = $fourthVariable + 7 | |
$fourthVariable++; // equivalent to $fourthVariable = $fourthVariable + 1 | |
echo 5*2+4; // 14 | |
echo 5+2*4; // 13 | |
echo (5*2+4)-(5+2*4); // 1 | |
echo (6*(2-4/2)+9)/(4/6+2); // 3.375 | |
echo 1/0; // Error, can't divide by zero! | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
Hello world
6.5
14
13
1
3.375
PHP Warning: Division by zero