-2

I am fairly new to php so if you have no patience for my questions I understand no need to be rude please just overlook my post thank you!

I am trying to make a form where a user inputs a price and discount percentage in order to see the sale price, this is what I have so far:

 <?php

$ogprice= $_POST['price'];
$discountper= $_POST['discount'];


function discountCalculations () {
  $saleprice = $ogprice - ($ogprice * ($discountper / 100));
  return $saleprice;
}

?>


<html>
 <title>Discount Calculator </title>
<body>

<h1>Discount Calculator</h1>
<form name="discountCalculator" action="functions.php" method="post">
 <input type="text" name="price"  placeholder="Original Price" ><br>
 <input type="text" name="discount" placeholder="Discount" ><br>
 <input type="submit" value="Calculate values"/>
 </form>


 <?php
  echo discountCalculations ();

 ?>
 </body>
 </html>

2 Answers2

0

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.

axiac
  • 68,258
  • 9
  • 99
  • 134
-1

the following html has the php function in a separate file. Move your php into a separate file where the welcome_get.php is (rename for your process), and modify this example from w3schools to your input and output.

<!DOCTYPE HTML>
<html>  
<body>

<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">

</body>
</html>
Joe McKenna
  • 135
  • 5
  • The question is about some code that doesn't work properly. Where is the code? – axiac Oct 10 '19 at 16:28
  • The question is with input how to output after php processing. This posted code is an example of input php process output. – Joe McKenna Oct 10 '19 at 16:34
  • 1
    Absolutely. Except there is no trace of PHP code in your answer :-D – axiac Oct 10 '19 at 16:42
  • I’m saying the problem isn’t the code, but the php needs to be in a separate file, a la ajax architecture where the client can’t display ‘processed data’ on the server without separating the php from the client. I updated my answer to make that clearer. – Joe McKenna Oct 10 '19 at 16:46
  • You cannot do a POST call and a HTML client in the same file, unless you use ajax architecture. – Joe McKenna Oct 11 '19 at 14:36