Reputation: 55
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?
Upvotes: 0
Views: 102
Reputation: 22817
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');
Upvotes: 0
Reputation: 1540
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"));
Upvotes: 2