DeanWhitehouse Posted October 15, 2008 Share Posted October 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/128601-solved-functions-with-unlimited-arguments/ Share on other sites More sharing options...
wildteen88 Posted October 15, 2008 Share Posted October 15, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/128601-solved-functions-with-unlimited-arguments/#findComment-666480 Share on other sites More sharing options...
DeanWhitehouse Posted October 15, 2008 Author Share Posted October 15, 2008 With that i assume i can do echo make_list(array('10','2')); etc? Or do i need to make vars Quote Link to comment https://forums.phpfreaks.com/topic/128601-solved-functions-with-unlimited-arguments/#findComment-666489 Share on other sites More sharing options...
DarkWater Posted October 15, 2008 Share Posted October 15, 2008 With that i assume i can do echo make_list(array('10','2')); etc? Or do i need to make vars You can directly pass in an array. Quote Link to comment https://forums.phpfreaks.com/topic/128601-solved-functions-with-unlimited-arguments/#findComment-666491 Share on other sites More sharing options...
wildteen88 Posted October 15, 2008 Share Posted October 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/128601-solved-functions-with-unlimited-arguments/#findComment-666494 Share on other sites More sharing options...
DarkWater Posted October 15, 2008 Share Posted October 15, 2008 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') ); Quote Link to comment https://forums.phpfreaks.com/topic/128601-solved-functions-with-unlimited-arguments/#findComment-666496 Share on other sites More sharing options...
DeanWhitehouse Posted October 15, 2008 Author Share Posted October 15, 2008 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 } Quote Link to comment https://forums.phpfreaks.com/topic/128601-solved-functions-with-unlimited-arguments/#findComment-666504 Share on other sites More sharing options...
wildteen88 Posted October 15, 2008 Share Posted October 15, 2008 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 } Quote Link to comment https://forums.phpfreaks.com/topic/128601-solved-functions-with-unlimited-arguments/#findComment-666508 Share on other sites More sharing options...
DeanWhitehouse Posted October 15, 2008 Author Share Posted October 15, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/128601-solved-functions-with-unlimited-arguments/#findComment-666510 Share on other sites More sharing options...
wildteen88 Posted October 15, 2008 Share Posted October 15, 2008 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') ) ); Quote Link to comment https://forums.phpfreaks.com/topic/128601-solved-functions-with-unlimited-arguments/#findComment-666515 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.