Skip to content

Instantly share code, notes, and snippets.

@recck
Created August 22, 2012 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save recck/3421441 to your computer and use it in GitHub Desktop.
Save recck/3421441 to your computer and use it in GitHub Desktop.
Week 1 - Day 2 - PHP Syntax, Variables and Mathematical Operations
<?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!
?>
@recck
Copy link
Author

recck commented Aug 22, 2012

Output:

Hello world
6.5
14
13
1
3.375
PHP Warning: Division by zero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment