0

In PHP is there a way to pass variable arguments as a single array without requiring an array declaration?

I'm calling a function below like:

$this->layouts->add_include(array('css/style.css','css/internal.css'));

Is there any way in the function to make it accept multiple variable arguments without needing to declare the array like:

$this->layouts->add_include('css/style.css','css/internal.css');

If I do this above then I get the following error in my functions foreach loop using the second code sample:

 Invalid argument supplied for foreach()

For reference the function is:

public function add_include($array)     
{
$asset_path = 'assets/';
  foreach($array as $array_item) {

    $this->CI->load->helper('url'); // Just in case!
    $this->includes[] = base_url() . $asset_path . $array_item;
}

1 Answer 1

1

func_get_args() can be used here

public function add_include()     
{
  $asset_path = 'assets/';
  foreach(func_get_args() as $array_item) {

    $this->CI->load->helper('url'); // Just in case!
    $this->includes[] = base_url() . $asset_path . $array_item;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant -- I knew about it, but didn't realize it'd work in this context. Thanks so much.

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.