0

I have this code and it works:

<html>
    <head>
        <title>Exercitii</title>
    </head>
    </head>
    <body> 
        <?php 
            function no_name($paramOne) { 
                $param = array("mihai", "jon", "michael");
                $param[] = $paramOne; 
                foreach($param as $value) { 
                    echo($value . " sunt cel mai bun " . "<br/>"); 
                } 
            } 
            $string = "dan";
            no_name($string);
        ?> 
    </body> 
</html>

Output:

mihai sunt cel mai bun
jon sunt cel mai bun
michael sunt cel mai bun
dan sunt cel mai bun 

But how can I add more names like: "costel", "mihaela", "george" to one array and call function with array parameter to further update names?

3
  • Please give me more options Commented Jul 24, 2014 at 9:06
  • So you want to add to the array $param every time the function is called? Commented Jul 24, 2014 at 9:13
  • yes..and add more name Commented Jul 24, 2014 at 9:19

2 Answers 2

2

If i understood correctly, you're trying to pass an array to the function instead of a string? If so, instead of appending to the $param array, you could do an array merge.

function no_name(array $paramOne) { 
    $param = array("mihai", "jon", "michael");
    $param = array_merge($param, $paramOne); 
    foreach($param as $value) { 
        echo($value . " sunt cel mai bun " . "<br/>"); 
    } 
} 

no_name(array("dan", "costel", "mihaela", "george"));
Sign up to request clarification or add additional context in comments.

1 Comment

Thaks all for your help...and Thanks Vlad your solution is very good
0

I would just pass the whole array to the function, without hardcoding anything in it.

But if you want to stick with your method, use func_get_args():

function please_call_me_everything_but_no_name(){

  static $defaults = ['some', 'names'];

  foreach(array_merge($defaults, func_get_args()) as $value){

    // do your stuff

  }

}

And you call it as:

please_call_me_everything_but_no_name('other', 'words');

7 Comments

If that's really what OP is looking for, I don't get the point of having such a function. By the way +1 "please_call_me_everything_but_no_name"
how can we use like foreach(array_merge($defaults, func_get_args())){ ??
@CodeLover by typing and running it? : PP Try yourself and see what happens, it won't hurt!
@CodeLover I forgot to add as $value, sorry.
@moonwave99 ur edit makes some sense now ..and it hurts a little by seeing ur first code ..:p
|

Your Answer

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