...

View Full Version : Function parameter is empty



surreal5335
12-03-2010, 11:25 PM
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:



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:



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/add_to_cart.php?id=1

I appreciate the help

Lamped
12-03-2010, 11:28 PM
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.

Fou-Lu
12-03-2010, 11:30 PM
This is a scope problem.


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.


BTW, the use of scope in PHP is actually pretty much identical to every C based language.

Lamped
12-03-2010, 11:35 PM
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... >.>

Fou-Lu
12-03-2010, 11:42 PM
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


And objects. I'd hate not having those too

surreal5335
12-04-2010, 10:30 AM
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

Fou-Lu
12-04-2010, 04:37 PM
The signature of a function is the declaration of it:


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.


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.


function product_exists($product_id, $layout)

And called with


$result = product_exists($someProdID, $someLayoutVar);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum