I understand I can define a function like this:
<?php
function say_hello() {
echo "hello world!";
}
say_hello();
?>
…and then reuse this function elsewhere as many times as I need by just calling the function by name.
What I don’t understand is “passing data to arguments within the function”. I’m confused what’s meant by within.
From one of the lessons I’ve been studying, I have this function:
<?php
function say_helloTWO( $word ) {
echo " {$word} hello world a second time ";
}
say_helloTWO( "my name is mike");
?>
This prints on screen
my name is mike hello world a second time
When I test the function with the argument "my name is mike", it prints to screen, but I don’t understand how this works. The variable $word wasn’t declared anywhere, so, if I take out the "$word" from the echo, then only "hello world a second time” shows without the my name is mike.
This leads me to believe that somewhere within this block of code, $word was defined, is that right?
In your second function
$wordis being declared in the function definitionfunction say_helloTWO( $word ).So the function is expecting 1 parameter, which will be assigned to the variable
$wordfor use within that function.So when you call
say_helloTWO( "my name is mike");you are passing 1 parameter (“my name is mike”) to the functionsay_helloTWO. This 1 parameter gets assigned to the 1st variable in the function definition, and is therefore available within the function as$word.Make sense?