I have this array:
$params = array(
'video' => 'xIihy8kkF',
'button' => 'View here',
'title' => 'Video title'
)
Now I want to create a function that shows some elements:
function show_video($params){
echo '<div>Video ID:' . $params['video'] . '</div>';
echo '<div>Button:' . $params['button'] . '</div>';
echo '<div>Title:' . $params['title'] . '</div>';
}
Finally I call the function show_video()
. But I get an error:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function show_video()
What am I doing wrong?
I have tried changing the function parameters like this:
function show_video($video = 'xIihy8kkF', $button = 'View here', $title = 'Video title'){
// here goes the content
}
I just don't want to include all the parameters inside the function name. I want to include them first in a variable array, and then use this array in the function name. Just like I did with function show_video($params)
, but that is not valid I suppose.
Any suggestions?
My Answer:
> function myFunction($params) {
> // Access the values in the array
> $video = $params['video'];
> $audio = $params['audio'];
> $image = $params['image'];
>
> // Do something with the values
> echo "Video: $video, Audio: $audio, Image: $image";
> }
>
> // Call the function with the array as parameter
> $params = array('video' => 'video.mp4', 'audio' => 'audio.mp3', 'image' => 'image.jpg');
> myFunction($params);
Rate this post
4 of 5 based on 3065 votesComments