Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/168532-solved-function/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/168532-solved-function/#findComment-889041
Share on other sites

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);

   

Link to comment
https://forums.phpfreaks.com/topic/168532-solved-function/#findComment-889042
Share on other sites

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 
}

Link to comment
https://forums.phpfreaks.com/topic/168532-solved-function/#findComment-889046
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.