Jump to content

[SOLVED] Functions with unlimited arguments


DeanWhitehouse

Recommended Posts

Is it possible to have a function with unlimited arguments?

 

e.g.

 

function one($var)

{

return $var;

}

 

echo one($var1,var2);

 

Something like that, so i can pass as many vars as i want to it, as it will be doing the same thing to all arguments.

 

 

You could use function_get_args(). Have a look at the example from the manual.

 

Alternatively just pass an array to your function, eg

 

$arr = array('one', 'two', 'three');

function make_list($arr)
{
    if(is_array($arr))
    {
        $list = '<ol>';
        foreach($arr as $item) $list .= '<li>'.$item.'</li>';
        $list .= '</ol>';

        return $list;
    }
    return false;
}

echo make_list($arr);

yes that is fine. However for readability, it'll be better to construct your array before calling the function (as I have shown). Rather than construct the array when you call the function.

 

That's a bit of a waste of a variable in some cases, as long as you properly space your code:

make_list(array('foo' => 'bar',
                        'something' => 'something',
                        'test' => 'we get the point')
              );

Last thing

If i use a multi-dimensional array how do i echo it out in a for each loop,

 

e.g.

<?php
$var = array(array('Home','index'));

foreach($var as $nav)
{
	?>
	<div style="background:#FFFFFF; border:#999999 thin solid; margin-top:2px; text-align:right; width:300px">
		<?php echo $nav; ?>
	</div>
	<?php
}

You'll have to use a foreach loop within a foreach loop

$var = array(array('Home','index'));

   foreach($var as $nav)
   {
      ?>
      <div style="background:#FFFFFF; border:#999999 thin solid; margin-top:2px; text-align:right; width:300px">
         <?php foreach($nav as $title) echo $title; ?>
      </div>
      <?php
   }

I tried that

<?php
function create_nav($link_name)
{
foreach($link_name as $nav)
{
	?>
	<div style="background:#FFFFFF; border:#999999 thin solid; margin-top:2px; text-align:right; width:300px">
		<a href="<?php foreach($nav as $nav_link){ echo $nav_link; }?>"><?php echo $nav; ?></a>
	</div>
	<?php
}
}
	create_nav(array(array('Home','index')));
?>

 

And that prints out

<div style="background:#FFFFFF; border:#999999 thin solid; margin-top:2px; text-align:right; width:300px">

<a href="Homeindex">Array</a>

</div>

Adapted your code a bit.

function create_nav($nav_links)
{
   foreach($nav_links as $link)
   {
      ?>
      <div style="background:#FFFFFF; border:#999999 thin solid; margin-top:2px; text-align:right; width:300px">
         <a href="<?php echo $link['url']; ?>"><?php echo $link['title']; ?></a>
      </div>
      <?php
   }
}

      create_nav(array( array( 'title' => 'Home',
                               'url' => 'home.html'),

                        array( 'title' => 'About',
                               'url' => 'about.html'),

                        array( 'title' => 'Contact',
                               'url' => 'contact.html')

                      )
                );

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.