The $variable in hello($variable) is called a 'parameter' or 'argument'. It allows you to pass a variable to a function so that the function can use its value.
Code: Select all
function hello() {
echo "Hello there!";
}
OR THIS:
function hello($name) {
echo "Hello there $name!"; // print the message with the given name
}
Now when the first function is called, the you would do this:
hello(); // this will print "Hello there!"
If you use the second function, then this will happen:
hello("Patriot"); // this will print "Hello there Patriot!"
There is yet ANOTHER option:
Code: Select all
function hello ($name = false) {
if ($name !== false) // this means that a name was given .. hello("name");
echo "How are you doing today $name?";
else // a name was NOT given .. hello();
echo "Hello there!";
}
Now you can have a function which has an optional argument. So if you call hello(), then you will see "Hello there!". If you pass a variable to the function... hello("Patriot"), then you will see "How are you doing today Patriot?"
Hope this makes things a bit clearer for you