There is a task

Write a script that, using the entered number K, displays the phrase "In the basket K of goods", and coordinates the end of the word "product" with the number K (for example, "There is 1 product in the basket" or "4 goods in the basket")

I wrote the code, everything works. That's just the problem, since I have a form and a script on the same page, as soon as I run it and until the first value is entered, I get an error:

Notice: Undefined index: k in C:\www\htdocs\1hw.php on line 31

Here is my 31st line:

 $k=$_POST["k"]; 

That is, it tries to take a value that has not yet been entered. After the first click on the submit button, everything works fine, the error goes away. How do I fix it before the first click?

    2 answers 2

    Check if the global variable has arrived:

     if(isset($_POST["k"])){ $k=$_POST["k"]; // что-то делаем } 
    • Thank. It helped - Oleggg

    and easier so

     $k = isset($_POST["k"]) ? $_POST["k"] : null;