简体   繁体   中英

Php user defined functions, (im new need help)

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 $word is being declared in the function definition function say_helloTWO( $word ) .

So the function is expecting 1 parameter, which will be assigned to the variable $word for 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 function say_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?

In this case the function definition is said to take one arguments.

function say_helloTWO( $word ) // $word is the name of this argument

When you call the function in your code the first thing you pass in the brackets will be passed as the first argument. Therefore in your example the string "my name is mike" is being passed into the function and assigned to the variable $word.

If we expand on this and have a function that takes two arguments like this

function say_helloTHREE( $word, $secondArg )

Then this function would now expect two arguments to be passed into it. The first argument would be assigned to $word and the second argument will be assigned to $secondArg.

This counts as a valid declaration. It's defined when supplied as an argument when the function is called.

<?php
    function my_name_is ($name) {
        echo "Hello, my name is " . $name;
    }
    my_name_is('mike');
    // returns 'Hello, my name is mike'
?>

Because, when calling the function, they supply 'mike' as an argument, $name becomes 'mike'. It's just how functions work.

EDIT:

It's a special rule. $name doesn't need to be declared because it will be defined when the function is called. PHP understands this. If you don't supply an argument, it's still defined, it's just empty... I believe.

<?php 
   function say_helloTWO( $word ) {
       echo " {$word} hello world a second time ";
   }

   say_helloTWO( "my name is mike");
?>

In this code, the 'declaration' of the variable $word is made within the function declaration. As you see on this line function say_helloTWO( $word ) { you can find $word on that line. This is enough for the PHP parser to know that for the first agument of the is to be known as word within this functions scope. The function then echos out that variable on a later line echo " {$word} hello world a second time "; where again $word is present prints out both what you set as the value of the first argument on this line say_helloTWO( "my name is mike"); .

Good question.

It's passed as a parameter. So it was sorta already declared inside the parenthesis

If you did:

function say_helloTWO( $word1, $word2, $word3 )

and called:

say_helloTWO("a", "b", "c");

That would call say_helloTWO and set the variable $word1 to "a" , $word2 to "b" and $word3 to "c"

Those three variables are defined in say_helloTWO for the duration that that function is running.

To give a practical example, say you had a function to solve quadratic equations, it would look like

function quadratic( $a, $b, $c ) { 
    $x = ( $b + sqrt( (b*b - 4*a*c) ) )/ 2*a;
    return $x;
}

Then, instead of rewriting the code to solve a quadratic each time, you could call it like this:

$y = quadratic (1, 2, 3);

This would set $a to 1, $b to 2 and $c to 3, and store the result in $y.

You can consider the quadratic function to be a sort of "mini-program", independent of the rest of your program, $a, $b and $c only apply within the quadratic function.

this leads me to believe that somewhere within this block of code, $word was defined, is that right?

No, $word is not defined within the function block, it is given the value of "my name is mike" when you mane the call.

In other words, $word is defined when you call the function:

/* here you are defining word to "my name is mike" */
say_helloTWO( "my name is mike");

/* here you are defining word to "my name is jack"*/
say_helloTWO( "my name is jack");

If the above still don't make much sense to you then I think you may need to go back to the basic and study what is a function in programming world.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM