Executing function wrong -- please help!

Discussion in 'PHP' started by Crayz, Jun 23, 2007.

  1. #1
    Hello, I have a code similar to below:

    <?
    
    function loggedin() {
       echo 'blah';
    }
    
    $home['loggedin'] = notloggedin();
    
    ?>
    Code (markup):
    My problem is, I only want the array, $home['loggedin'], to execute that function when the array is called, I think the word would be.

    Example, if I echo 'd the array, that is the only time I would want it to execute.

    Instead, the array executes the function right away without being called.

    Suggestions?

    Thanks!
     
    Crayz, Jun 23, 2007 IP
  2. SeLfkiLL

    SeLfkiLL Active Member

    Messages:
    85
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    50
    #2
    If you want an array of functions, this is the correct syntax:

    
    function loggedin() {
       echo 'blah';
    }
    
    $home['loggedin'] = 'loggedin';
    
    $home['loggedin'](); //this will output 'blah'
    
    PHP:
     
    SeLfkiLL, Jun 23, 2007 IP
  3. Crayz

    Crayz Well-Known Member

    Messages:
    708
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    120
    #3
    Woops, I typed in my code wrong :eek:

    <?
    
    function loggedin() {
       echo 'blah';
    }
    
    $home['loggedin'] = loggedin(); // it was notloggedin();
    
    ?>
    PHP:
    I am testing your code now, thanks!
     
    Crayz, Jun 23, 2007 IP
  4. Crayz

    Crayz Well-Known Member

    Messages:
    708
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    120
    #4
    Alright, not quite what I was looking for.

    function loggedin() {
       echo 'blah';
    }
    
    $home['loggedin'] = 'loggedin';
    
    $home['loggedin'](); //this will output 'blah' 
    PHP:
    If anyone is familiar with vBulletin, inside templates you just have to type $home[loggedin] and it will output the contents of that array.

    Well, what I need is to place my function inside that array, and call that array inside a vbulletin template using $home[loggedin] so that it executes the function.

    If anyone knows how to call a function inside vbulletin templates, that may also work.

    Thanks.
     
    Crayz, Jun 23, 2007 IP
  5. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well, try to work with return than!

    
    <?
    function loggedin() {
       return 'blah';
    }
    
    $home['loggedin'] = loggedin(); // it was notloggedin();
    // after the above line $home['loggedin'] will contain "blah". In vBulletin, if you put $home[loggedin] in your templates, it will output blah.
    ?>
    PHP:
    Hope this helps.
     
    DeViAnThans3, Jun 24, 2007 IP