Treehouse: Grow your CSS skills. Land your dream job.

use function variables like wordpress

  • # July 21, 2010 at 11:13 pm

    hey guys, what i mean about the variables is something like:


    how can I make something like this work on my website?

    # July 22, 2010 at 2:04 pm

    you need to build a function then pass the data into it :)

    for example:

    function greet($name=NULL) {

    return "Hello $name";


    Then you call the function and pass in the name…

    echo greet("Makeshift");

    //Outputs: "Hello Makeshift"

    # July 22, 2010 at 5:34 pm

    I know that, but how do i get the function to get each variable passed using ampersands and equal signs like that?

    I made this little script for it:

    function _li($args){
    $args = explode(‘&’, $args);

    $args1 = $args[0];
    $args1 = explode(‘=’, $args1);
    $args1 = array($args1[0] => $args1[1]);
    foreach($args1 as $key => $val){
    if($key == ‘text’){$text = $val;}
    elseif($key == ‘title’){$title = $val;}
    elseif($key == ‘href’){$href = $val;}

    $args2 = $args[1];
    $args2 = explode(‘=’, $args2);
    $args2 = array($args2[0] => $args2[1]);
    foreach($args2 as $key => $val){
    if($key == ‘text’){$text = $val;}
    elseif($key == ‘title’){$title = $val;}
    elseif($key == ‘href’){$href = $val;}

    $args3 = $args[2];
    $args3 = explode(‘=’, $args3);
    $args3 = array($args3[0] => $args3[1]);
    foreach($args3 as $key => $val){
    if($key == ‘text’){$text = $val;}
    elseif($key == ‘title’){$title = $val;}
    elseif($key == ‘href’){$href = $val;}

    (there’s more to it, but this splits the function variable)

    and the usage..

    _li(‘text=Home&title=Go Back Home’);

    but this will only take the first 3 variables, of course I can make it as many as I want, but every time i make a new function for something like this, I don’t wanna have to set it for how many variables it’s looking for…

    # July 22, 2010 at 7:54 pm

    Well – is there any reason why you need to pass them like that firstly? Could you not just create an array and throw that in?

    Because I am sure you know this already, that format is how your browser will POST and GET… I am not sure how WP are doing it in their code but I suppose you could explode the data into an array and use it that way…

    $var = explode(‘&’, $string);

    That would give you an array like


    Then you are atleast half way there – maybe then you could split them again…


    Was just playing around with a double explode idea and thought I would check the info on explode – and I found this awesome function :D

    function doubleExplode ($del1, $del2, $array){

    $array1 = explode("$del1", $array);

    foreach($array1 as $key=>$value){
    $array2 = explode("$del2", $value);
    foreach($array2 as $key2=>$value2){
    $array3[] = $value2;

    $afinal = array();

    for ( $i = 0; $i <= count($array3); $i += 2) {
    $afinal[trim($array3[$i])] = trim($array3[$i+1]);
    return $afinal;

    So you basically use it like this…

    $string = "title=home&foo=bar&this=that";
    $myVar = doubleExplode(‘&’, ‘=’, $string);

    and you get back a nice array:

    [title] => home
    [foo] => bar
    [this] => that

    Is that more the kind of thing you were looking for? :)

    credits to this awesome post:

    # July 23, 2010 at 6:51 am

    oh, wow. works like a charm :) thank you very much.

    # July 23, 2010 at 12:21 pm

    "Double Explode" .. We work in an amazing industry.

    # July 24, 2010 at 5:34 am

    lol nice :D

    At least they didn’t call the function "boomBoom" lol

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.