Skip to content

Instantly share code, notes, and snippets.

@KyeRussell
Created June 7, 2013 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyeRussell/5730482 to your computer and use it in GitHub Desktop.
Save KyeRussell/5730482 to your computer and use it in GitHub Desktop.
Argument passing for dummies, for somebody else. Gross oversimplifications.
<?php
/* Let's say we have this function, it just adds one to a
* number and returns the resulting value.
*
* It takes in a paramater, which we call $number.
*
* $number is a variable which only exists within the function, when we try
* to access $number from outside the function, it simply doesn't exist.
* $number simply refers to whatever we've passed to addOneToNumber. We use
* it inside addToNumber to refer to what you've passed to it.
*/
function addOneToNumber($number)
{
$newNumber = $number + 1;
return $newNumber;
}
// we can call it, passing in whatever we want.
// this includes constant values (like numbers).
$var1 = addOneToNumber(22.2); // var1 would equal 23.2
// you can pass in variables.
$pi = 3.14159
$piPlus1 = addOneToNumber($pi); // $piPlus1 would be 4.14159
// you could even pass in the result of another function.
function takeOneFromNumber($number)
{
$new = $number - 1;
return $new;
}
addOneToNumber(takeOneFromNumber(2)); /* this would equal 2, because we're getting the OUTPUT of
* takeOneFromNumber() (what it returns) and feeding it TO
* addOneToNumber(), for example.
*
* takeOneFromNumber(2) will equal 1.
* addOneToNumber(1) will equal 2.
*
* Since we're taking one from a number, and adding it back
* on again, we're getting the same number (hopefully).
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment