DarkPrince2005 Posted August 2, 2009 Share Posted August 2, 2009 Ok I'm just trying to return a value depending on an if statement. Is there something wrong with my function or what? function country_amount() { $a=run_query("SELECT userid,country FROM users_details where userid like '149'"); //return $a['country']; if ($a['country'] == 'ZA') { $netcashgross1 = "55"; //return $netcasrhgross1; global $netcashgross; } else //if($a['country'] != 'ZA') { $netcashgross1 = "120"; //return $netcashgross1; global $netcashgross; } return $netcashgross1; } return code <?php include "functions.php"; //include "config.php"; country_amount(); //$a=$netcashgross; echo "$netcashgross1"; ?> it returns something but never what the if statement says. Quote Link to comment https://forums.phpfreaks.com/topic/168532-solved-function/ Share on other sites More sharing options...
DarkPrince2005 Posted August 2, 2009 Author Share Posted August 2, 2009 it continues to only return 120, doesn't matter what i change the userid to. Quote Link to comment https://forums.phpfreaks.com/topic/168532-solved-function/#findComment-889039 Share on other sites More sharing options...
BLaZuRE Posted August 2, 2009 Share Posted August 2, 2009 First of all, if you want a value returning function, you need to GIVE the function values using parameters. These values go inside the () of the function. For the actual function, use: function nameFunction($parameter1,$parameter2) {...} where $parameter1 and $parameter2 are the new names those values will become when you call the function and ... is just whatever code you want inside. The return statement needs to be INSIDE the function to return stuff from inside the function (a return statement ends your function execution). Anything outside the function will be executed from top-to-bottom, left-to-right, like always (functions are excluded from this method, they're only executed when called). You can have any number of parameters (0 to infinite). To call the function, use: nameFunction("Here is some text",2005); each parameter must be separated by a comma. Also, you may want to do echo $variable; instead of echo "$variable"; . It's slightly more efficient and variables don't have to be in double quotes. Try going to http://www.w3schools.com/PHP/php_functions.asp to better understand how to use functions. Quote Link to comment https://forums.phpfreaks.com/topic/168532-solved-function/#findComment-889041 Share on other sites More sharing options...
ignace Posted August 2, 2009 Share Posted August 2, 2009 Create all conversions 100-based so you can use them as percentages, use google: 100 usd to <currency> Then divide currency through 100 (<currenty>/100) the currency conversion for usd to eur = 0.7 function country_amount($netcashgross) { global $cconv;/*currency conversions*/ $r = run_query('SELECT userid,country FROM users_details WHERE userid =149'); $country = strtoupper($r['country']); $conv = !empty($cconv[$country]) ? $cconv[$country] : 1; return $netcashgross * $conv; } print country_amount($netcashgross); Quote Link to comment https://forums.phpfreaks.com/topic/168532-solved-function/#findComment-889042 Share on other sites More sharing options...
DarkPrince2005 Posted August 2, 2009 Author Share Posted August 2, 2009 How would you write the function? Cause I'm having trouble underrstanding what you mean. function country_amount() { $a=run_query("SELECT userid,country FROM users_details where userid like '149'"); //return $a['country']; if ($a['country'] == 'ZA') //or whatever South Africa is defined as in the database { $netcashgross = "55"; //return $netcashgross; //if return value is required. Leave out if not //global $netcashgross; //if return value is required. Leave out if not } else //if($a['country'] != 'ZA') { $netcashgross = "120"; //return $netcashgross; //if return value is required. Leave out if not //global $netcashgross; //if return value is required. Leave out if not } return $netcashgross; //if return value is required. Leave out if not } Quote Link to comment https://forums.phpfreaks.com/topic/168532-solved-function/#findComment-889046 Share on other sites More sharing options...
DarkPrince2005 Posted August 2, 2009 Author Share Posted August 2, 2009 I dont want to convert the currency, the 55 and 120 are fixed vales Quote Link to comment https://forums.phpfreaks.com/topic/168532-solved-function/#findComment-889047 Share on other sites More sharing options...
BLaZuRE Posted August 2, 2009 Share Posted August 2, 2009 Have you tried using print_r to print your query and see its contents? Maybe nothing is equal to ZA that is returned from you query? Quote Link to comment https://forums.phpfreaks.com/topic/168532-solved-function/#findComment-889051 Share on other sites More sharing options...
DarkPrince2005 Posted August 2, 2009 Author Share Posted August 2, 2009 I unfortunately know for a fact that there is, the database only has 2 entries one equal to ZA and another not. For testing purposes. Quote Link to comment https://forums.phpfreaks.com/topic/168532-solved-function/#findComment-889057 Share on other sites More sharing options...
ignace Posted August 4, 2009 Share Posted August 4, 2009 I dont want to convert the currency, the 55 and 120 are fixed vales You can't use fixed values unless you only have 1 product to sell or are all your products labeled 55 USD? Quote Link to comment https://forums.phpfreaks.com/topic/168532-solved-function/#findComment-890444 Share on other sites More sharing options...
TeNDoLLA Posted August 4, 2009 Share Posted August 4, 2009 Do a var_dump($a) inside the function and outside the if clause and paste the results here. Quote Link to comment https://forums.phpfreaks.com/topic/168532-solved-function/#findComment-890447 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.