Page 1 of 1

function hello($variable)?

Posted: Wed Jul 17, 2002 9:51 pm
by Patriot
exactly whats the difference between these:

Code: Select all

function hello();
and

Code: Select all

function hello($variable);

and (if anything) what does the $variable do between the brackets?
could someone point me to a tutorial or give me a short briefing.

Posted: Wed Jul 17, 2002 10:05 pm
by protokol
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