Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    Function parameter is empty

    I have a function that calls in another function which is where my problematic parameter is being stored. The variable is avlue which is pulled from the current url (this could also be the cause of the problem).

    Here is my code:

    Code:
    function get_xml_ebooks($layout)
    {
    return new SimpleXMLElement(file_get_contents($layout));
    }
    
    function product_exists($product_id)
    {
    	foreach(get_xml_ebooks($layout) as $product)
    	{
    		if ($product->id == $product_id)
    		{
    			return true;
    		}
    	}
    	return false;
    }
    $layout is the empty variable in question. It holds the name of an xml file to be called based on the current url. I got this working on the other page showing the catalog, but my shopping cart page didnt seem to transfer all the data over, so $layout is now blank... (probably bc the url has changed)


    Here is my switch that changes the values of $layout:

    Code:
    switch ($url) {
    
    	case "marketing":
    		$layout = MARKETING_XML;
    	break;
    	
    	case "business":
    		$layout = BUSINESS_XML;
    	break;
    	
    	case "ebooks":
    		$layout = CREATE_EBOOKS_XML;
    	break;
    
    		
    }
    $url works just fine, no worries there.

    Here is my web page displaying all the errors I am getting regarding this missing value during the function call:

    http://paperlesswasp.com/millionaire..._cart.php?id=1

    I appreciate the help

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP doesn't have the usual variable scope rules. A variable set in the global scope (your $layout) isn't available in functions. There are a few workarounds involving the global keyword, the $_GLOBAL superarray, or passing $layout as a function parameter.

    I tend to pass variables as a function parameter and avoid globals.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This is a scope problem.
    PHP Code:
    function product_exists($product_id)
    {
        foreach(
    get_xml_ebooks($layout) as $product)
        {
            if (
    $product->id == $product_id)
            {
                return 
    true;
            }
        }
        return 
    false;

    You are calling get_xml_ebooks using $layout has an argument. $layout has not been defined by this point, regardless of if it is available in your main. Functions do not carry over values of previously defined variables unless they are superglobals (which is why they were named superglobals).

    Modify your signature to accept what the $layout variable is to pass into the product_exists page. Do not use a global approach to this; you should avoid the use of global unless you are dealing with a function signature that cannot be modified (for use in PHP delegates like usort or set_error_handler) since PHP cannot overload.

    Edit:
    BTW, the use of scope in PHP is actually pretty much identical to every C based language.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Quote Originally Posted by Fou-Lu View Post
    Edit:
    BTW, the use of scope in PHP is actually pretty much identical to every C based language.
    Amusingly, I've been programming in various languages for 18 years, but never a C derivative... I keep meaning to, I've even bought books. Never quite got round to it... >.>
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep, and despite having pretty heavy Perl roots too, one of the things we didn't inherit was globals >.<
    Which is great though, I hate not having scope control O.o

    Edit:
    And objects. I'd hate not having those too
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks a lot for your help,

    not sure what you mean by "modify your signature". Would this be done in the function or in the switch that defines the value of $layout? It would make more sense to the latter seeing as it would seemingly be too late making changes in the function that is outside the variables scope.

    Thanks a lot

    Ben

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The signature of a function is the declaration of it:
    PHP Code:
    public void myfunc(int i); 
    The above signature (not PHP, but this is important to know in order to understand signatures) says that I have a function, called 'myfunc', that has 1 parameter which is an integer. It returns nothing. It is accessible from out of scope (an object oriented process which is the public, though procedural has their own set like static).
    PHP is more lenient since it is a datatype weak language.
    PHP Code:
    function product_exists($product_id
    Is a function that returns RETURNVAL (think of it as 'if' our function returns a result, it will be that value, otherwise it is null), takes a single required zval type variable called product_id (every variable in PHP is a zval or derivitive), and is available in every scope.

    Modifying the signature is when you change the declaration of the function to a different name, return type, scope, number of parameters, or type of parameters. Some languages can overload these, so you can share the function name over different signatures. PHP cannot overload, but instead allows optional variable.

    In order to get $layout into your function, you need to modify the signature and everything that calls it.
    PHP Code:
    function product_exists($product_id$layout
    And called with
    PHP Code:
    $result product_exists($someProdID$someLayoutVar); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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