Code Lover
Code Lover

Reputation: 8348

explode function in custom function

I am trying to create custom function for Main Nav menu where I can only write menu item name and it will automatically wrap with

  • my content
  • Also I want variable where I can define url for each menu item.

    below is my code and giving me Parse error: syntax error, unexpected T_VARIABLE

    function the_main_nav($navlinks){
            echo '<nav>';
            echo '<ul>';
    
            $menuitem = $navlinks;
            $pieces = explode("," $menuitem);
            echo $pieces[0];
            echo $pieces[1];
    
            echo $menuitem;         
            echo '</ul>';
            return $pieces;
        }
    

    ------------------[Modified code]----------------------

    function the_main_nav($navlinks){
    echo '<nav>';
    echo '<ul>';
    
    $menuitem = $navlinks;
    $pieces = explode(" ",$menuitem);
    echo '<li>';
        echo $pieces[0];
        echo '</li>';
    echo '<li>';
        echo $pieces[1];
        echo '</li>';
    
    echo '</ul>';
        echo '</nav>';      
    }
    

    Now I want to make it dynamic like instead of getting value from [0] [1]..so on i want it will automatic generate as per the input character and create list with li

    Upvotes: 0

    Views: 483

    Answers (2)

    cool.ernest.7
    cool.ernest.7

    Reputation: 93

    The explode() function breaks a string into an array. explode(separator, string) so in your code comma is missing.

    Upvotes: 0

    Shakti Singh
    Shakti Singh

    Reputation: 86406

    You are missing the ,. You have to separate the argument of explode using comma.

    $pieces = explode(",", $menuitem);
    

    Upvotes: 2

    Related Questions