#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Colorado
    Posts
    11
    Rep Power
    0

    Is it possible to do this: $variable = function()


    I have a simple php blog that I used to have a regular html menu on each page. I'm attempting to turn the menu into a function so I only have to update it from one place. Here is what I have so far:

    Code:
    <?php
            function menuDisplay($home_link, $admin_link, $forum_link, $logout_link)
            {
               echo $home_link . '  ' . $admin_link . '  ' . $forum_link . '  ' . $logout_link;  
            }
            $home_link = '<small><a href="index.php">home</a></small>';
            $admin_link = '<small><a href="admin/index.php">admin</a></small>';
    		$forum_link = '<small><a href="forum/index.php">forum</a></small>';
    		$logout_link = logoutLink() 
    
    ?>
    It works without the logout_link portion, which I need to kind of turn into a function of itself. This is what I was using to make the logout link work in just the regular html menu I had:

    Code:
    if(isset($_SESSION['logged_in'])) { echo '<small><a href="admin/logout.php">Logout</a></small>';
    So I'm trying to figure out how to transform that if statement into a function that I can call within the other function. Does that make sense?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Colorado
    Posts
    11
    Rep Power
    0
    Just a quick update, I solved my immediate problem by creating just a separate function altogether, but I would still like to figure out how to put them all into one function.

    Code:
    <?php
            function menuDisplay($home_link, $admin_link, $forum_link) {
               echo $home_link . '  ' . $admin_link . '  ' . $forum_link;  
            }
            $home_link = '<small><a href="index.php">home</a></small>';
            $admin_link = '<small><a href="admin/index.php">admin</a></small>';
    		$forum_link = '<small><a href="forum/index.php">forum</a></small>'; 
    		
    		function logoutLink($logout_link) {
    			if(isset($_SESSION['logged_in'])) {
    				echo $logout_link;
    			}
    		} 
    		$logout_link = '<small><a href="admin/logout.php">logout</a></small>';
    
    ?>
  4. #3
  5. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,065
    Rep Power
    9398
    I'm not sure what you have, don't have, and are trying to get. So the generic advice:

    You mention two functions. Are you ever going to use them separately? If so then you do need two functions, otherwise you can combine then into the one.

    As for variable problems, for example,
    PHP Code:
    function logoutLink($logout_link) {
        if(isset(
    $_SESSION['logged_in'])) {
            echo 
    $logout_link;
        }
    }
    $logout_link '<small><a href="admin/logout.php">logout</a></small>'
    You can't define $logout_link outside the function and then use it inside the function. It's not available. But since $logout_link is just a simple string you don't even need a variable at all:
    PHP Code:
    function logoutLink() {
        if(isset(
    $_SESSION['logged_in'])) {
            echo 
    '<small><a href="admin/logout.php">logout</a></small>';
        }

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Colorado
    Posts
    11
    Rep Power
    0
    The code I used for both functions works with me using the defining the variables outside the functions and then using them in the functions. I'm using it now on a live site.



    But yeah, I definitely like the way you did it better anyways, since it's just a string. Thanks a ton!

IMN logo majestic logo threadwatch logo seochat tools logo