Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: php function trouble

  1. #1
    Join Date
    Jul 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default php function trouble

    I am trying to create a function to load a file only once, then I want to call it multiple times in my code but it doesn't seem to be working.
    here is my function:

    PHP Code:
    <?php
        
    function load_sw_popup($xml){
        
    $xml=simplexml_load_file("sw-popup.xml");
        }
    ?>
    an example of one of the places I am calling it:

    PHP Code:
    <?php
           load_sw_popup
    ($xml);
           echo 
    $xml->recentPRs;
    ?>
    Last edited by jscheuer1; 07-19-2013 at 04:13 PM. Reason: Format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    By declaring $xml inside a function, I believe it's only available inside that function. If your desire is to create a GLOBAL, don't use a function. Instead just do:

    PHP Code:
    $xml=simplexml_load_file("sw-popup.xml"); 
    Then later for your other code, just do:

    PHP Code:
    <?php 
           
    echo $xml->recentPRs
    ?>
    I think there are ways to have the function make a GLOBAL though, check global in the man:

    http://php.net/manual/en/language.variables.scope.php

    It shows how to take a GLOBAL and act on it in a function. It might be able to be used to create a GLOBAL in a function. But, as I said, if you want a GLOBAL, the easiest thing is to just not use a function to create it. I think this would do it though:

    PHP Code:
    <?php  
        
    function load_sw_popup(){  
            
    $GLOBALS['xml']=simplexml_load_file("sw-popup.xml");
        }  
    ?>
    Then later:

    PHP Code:
    <?php 
           load_sw_popup
    (); 
           echo 
    $xml->recentPRs
    ?>
    Last edited by jscheuer1; 07-19-2013 at 05:04 PM. Reason: add final code block
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your help!

  4. #4
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Actually, better than using a global variable (which is normally advised against), it's probably better just to use a reference.

    Code:
    <?php 
    function load_sw_popup(&$xml){ 
        $xml = simplexml_load_file("sw-popup.xml"); 
    } 
    ?>

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    Nile, I may be missing something but I just tested that and it appears to simply be another syntax for creating a global variable:

    PHP Code:
    <?php  
        
    function load_sw_popup(&$xml){ 
            
    $xml='bob';  
        } 
        
    load_sw_popup($xml); 
        echo 
    $GLOBALS['xml']; //echoes bob
    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It's a fairly complicated topic:
    http://php.net/manual/en/language.references.pass.php
    The basic idea: you can send a variable into a function and actually manipulate it in that function. It's sort of like saying "pretend this isn't a new scope". But it's not global, because that can be within a few levels. This becomes relevant if you call functions from within functions, or, often, with OOP.

    It's not equivalent. You'll see that no longer works when embedded:
    PHP Code:
     <?php  
        
    function load_sw_popup(&$xml){ 
            
    $xml='bob';  
        } 
        function 
    savebob() {
            
    load_sw_popup($xml); 
        }
        
    savebob();
        echo 
    $GLOBALS['xml']; //echoes nothing, or gives an error
    ?>
    (You were coincidentally already in global scope. This is an example of why scope can be confusing.)

    Global variables should be used for system settings, for something that should always be set. (One problem can come from mixing in third-party scripts, in case of a name conflict, but that situation is relatively rare, not that it's easy to fix if it happens.)
    Global variables should not be used just to get a value from one thing to another. Personally I strongly prefer to use return to do all of this if possible. I don't really like using 'reference' unless required, and it usually isn't.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    It is equivalent for the example as first presented by Nile. In your example $xml isn't accessible anywhere I can see. You would have to make a getter function and/or $xml would have to be assigned as a property of something in order to be both accessible and not global.

    In my previous post I was going to add something like:

    To avoid using global variables you have to carry out whatever you are doing within the context of an object or function.

    In other words, just using &$whatever doesn't prevent a global variable from being created. It's the context or scope of the function or object that determines that. But that wasn't the original question. Essentially the OP had created $xml in a limited local scope and was wondering why it wasn't available in the global scope.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    $xml would be available within the savebob() function. That's why it's not global. But it's the same $xml variable then within savebob() and also load_sw_popup() because it's sent by reference to load_sw_popup().
    I was just replying to your reply to Nile to clear that up.


    As for the original question, I think it would be solved most simply like this:
    PHP Code:
     <?php
        
    function load_sw_popup(){
            return 
    simplexml_load_file("sw-popup.xml");
        }
    ?>

    <?php
           $xml 
    load_sw_popup();
           echo 
    $xml->recentPRs;
    ?>
    Simple as that. At least that solves the immediate problem.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    If part of the original question is loading the file only once, here's how you could modify Daniel's function to do it:

    PHP Code:
     <?php

        
    // this causes the function to load the xml file
        
    $xml load_or_get_sw_popup();

        
    // this causes the function to return the previously loaded xml file
        // (it does _not_ load it again)
        
    $xml2 load_or_get_sw_popup();

        function 
    load_sw_popup(){
            
    // static variables persist (are not destroyed) between function calls
            
    static $xml;
            
    // if this is the first time we call the function, $xml will be NULL
            
    if( ! $xml ){
                
    // so we load the file
                
    $xml simplexml_load_file("sw-popup.xml");
            }
            return 
    $xml;
        }

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by traq View Post
    If part of the original question is loading the file only once
    Yes, I was wondering that too.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Adding javascript function within ontoggle function
    By piers in forum Dynamic Drive scripts help
    Replies: 6
    Last Post: 07-22-2009, 03:41 AM
  2. Trouble passing variables to another function
    By polyglot in forum JavaScript
    Replies: 10
    Last Post: 06-05-2009, 03:17 PM
  3. Replies: 9
    Last Post: 09-16-2008, 06:31 PM
  4. Trouble with function not receving params in FF
    By changemystrings in forum JavaScript
    Replies: 4
    Last Post: 07-27-2008, 01:53 AM
  5. Passing Variables Function to Function
    By jscheuer1 in forum JavaScript
    Replies: 3
    Last Post: 03-26-2005, 12:22 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •