The function discountCalculations()
returns 0
for all inputs because the variables $ogprice
and $discountper
are not initialized (they actually don't exist when they are used).
PHP is actually telling you this using a notice:
PHP Notice: Undefined variable: discountper
but, most probably, your PHP interpreter is configured (by default) to ignore the notices.
(It is wrong but this is a different subjects.)
In PHP there are two variables scopes: a local scope (that belongs to each function and only the code of that function can access it) and the global scope.
Unlike other languages where the global scope is accessible from everywhere, in PHP the global scope can be accessed by default only from the global code. This is the code outside any function or class method.
The lines:
$ogprice = $_POST['price'];
$discountper = $_POST['discount'];
create the variables $ogprice
and $discountper
in the global scope (because they are outside any function.)
By default, the code of function discountCalculations()
cannot see the global variables $ogprice
and $discountper
. When it reaches the line:
$saleprice = $ogprice - ($ogprice * ($discountper / 100));
the variables $ogprice
and $discountper
do not exist in the local scope of the function. PHP displays three notices about them (one for each usage of an undefined variable) and uses NULL
instead. When used in numeric context, NULL
is converted to the number 0
and the calculation becomes equivalent to:
$saleprice = 0 - (0 * (0 / 100));
Obviously the result is always 0
.
A function can access the global scope but I won't tell you how to do it. Using global variables is bad practice in any language.
Instead, I recommend you to make your function accept as parameters the values to work with:
function discountCalculations ($price, $discount) {
$saleprice = $price - ($price * ($discount / 100));
return $saleprice;
}
Now, $price
and $discount
are parameters of the function. The function parameters belong to the local scope of the function but they are initialized with the values used to invoke the function:
$ogprice = $_POST['price'];
$discountper = $_POST['discount'];
echo discountCalculations($ogprice, $discountper);
This way you can reuse the function with different values and it does not have hidden dependencies. It is much clear for anyone that reads the code that the function needs two values to do its job.
Read more about variables scopes in the PHP documentation.
(Then forget anything you read in that page about the global
keyword because it does more harm than good to your code and your coding style :-) )
Also read about the various error levels in PHP and use error_reporting=E_ALL
in php.ini
or error_reporting(E_ALL)
in the code to enable the reporting of all errors.
Many notices (like "Undefined variable" or "Undefined array index") are not just some minor things that can be ignored but real coding errors. PHP still handles them as minor but since version 8.0 it will take them more seriously and report them as warnings. They are, in fact, real errors but reporting them as errors will break a lot of poorly written PHP code that accumulated in the last 20 years since PHP has been released.