1

I'm not really sure what this is called or how to search for it so hopefully this hasn't been asked too many times before.

Is it possible to pass a variable to a function inside the function variable thing... I know that doesn't make sense, so here is an example:

sendContact('Firstname Lastname', $email, $address);

function sendContact(splitWord($name), $email, $address) {
    //code here
    print_r($name);
    //result array[0] = 'Firstname';
    //result array[1] = 'Lastname';
}

function splitWord($name) {
    //code here to split words
    return $result
}

All I'm looking for is the sendContact(splitWord()) part. Is there a way to do that as that doesn't seem to work.

1
  • 2
    nope, but you can just call splitWord() inside of sendContact() Commented May 8, 2013 at 3:06

5 Answers 5

1

Yes. Don't put the function call in the definition, put it in the execution like so:

function sendContact( $name, $email ) {

}
function splitWord( $name ) {
  return $result;
}

sendContact( splitWord( $name ) );
Sign up to request clarification or add additional context in comments.

1 Comment

Of course.. Wow, I can't believe this didn't cross my mind. It must be that time of day.
1

Why not just remove some job from you?

sendContact('Firstname Lastname', $email, $address);

function sendContact($name, $email, $address) {
    $name = splitWord($name); // put inside to not duplicate for each call

    //code here

    print_r($name);

    //result array[0] = 'Firstname';
    //result array[1] = 'Lastname';
}

function splitWord($name) {
    //code here to split words
    return $result;
}

Comments

0

Perhaps you could call your other function during the call for your main.. See an example:

function sendContact($name, $email, $address) {
    //code here
    print_r($name);
    //result array[0] = 'Firstname';
    //result array[1] = 'Lastname';
}

function splitWord($name) {
    //code here to split words
    return explode (" ",$name); // Different, but made to prduce an array to show that it works
}

sendContact(splitWord('Firstname Lastname'), $email, $address);

I have called the splitWord() function during the same time as i'm calling the sendContact(); function

Comments

0

If you are 'real programmer', it's bad practice writing code like that. You should use OOP approach, if there are lots of functions involved inside a function.

class Email
{

    function splitWord($name)
    {
         // the jobs of split
    }

    function sendContact($name, $email, $address)
    {
         $receiver = $this->split($name);
         mail($receiver, $email, $address);
    }
}

2 Comments

"real programmer" not puting objects everywhere. OOP is good when it needed, not forced. "OOP just for OOP" is not good.
Those were just examples. The splitWord function is not specific only to the sendContact function. I just put them there to give context to what I was trying to achieve. Thanks though.
-1

Not really. The most straightforward solution might be a simple wrapper function:

function sendAndSplitContact($name, $email, $address) {
  return sendContact(splitWord($name), $email, $address);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.