简体   繁体   中英

PHP function loop through variables and output it

I'm trying to make a little php function so I can easily create and change links in my website. I am using a jQuery plugin for hash pages, which is what my website is driven on, all hash based website, so I use this a lot and at some point if I want to change the URL setup it would take forever to go through each place and do it.

I've got something like this right now, which would work for the first two variables, but the problem is there could just be one variable returned sometimes, and other times there could be 2, or 4, and in the future even more than the four i have defined.

function href($page, $pageID, $subPage, $subPageID)
{

$href = 'href="#/'.$page.'/'.$id.'" rel="address:/'.$page.'/'.$id.'"';

return $href;   
}

The finished links would look like these:

<a href="#/home" rel="address:/home"></a>

and or

<a href="#/profile/1" rel="address:/profile/1"></a>

and or

<a href="#/profile/1/subPage/2" rel="address:/profile/1/subPage/2"></a>

etc.

and I would like to accomplish this using the function, and the usage would be something along the lines of:

<a <?php echo href('home')?>></a>

and or

<a <?php echo href('profile', '1')?>></a>

and or

<a <?php echo href('profile', '1', 'subPage')?>></a>

etc.

Any help with how to solve this would be greatly appreciated, thanks!

Try this:

function href(){
    $url = implode('/', func_get_args());
    $href = 'href="#/'.$url.'" rel="address:/'.$url.'"';
    return $href;   
}

When calling href('hello', 'world', '1'); , it should return the string

href="#/hello/world/1" rel="address:/hello/world/1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM