-1

I am learning some PHP Stuffs. I am trying to add two number from a text box but it shows a error message. This error actually came from following <?php echo $c;?> line but it look like ok for me. I used "empty " tag <?php echo empty($c);?> to hide error but it didn't work. Why?

            <body>
            <?php
                if(isset($_POST['submit_btn']))
                {
                    $a=$_POST['no1'];
                    $b=$_POST['no2'];
                    $c=$a+$b;

                }
            ?>


                            <form method="post">
                            <table border="3">
                                    <tr>
                                        <td>Number 1</td>
                                        <td><input type="text" name="no1"/></td>
                                    </tr>
                                    <tr>
                                        <td>Number 2</td>
                                        <td><input type="text" name="no2"/></td>
                                    </tr>
                                    <tr>
                                        <td>Result</td>
                                        <td><input type="text" value="<?php echo $c;?>" /></td>
                                    </tr> 
                                    <tr align="center">
                                        <td colspan="2"><input type="submit" name="submit_btn"/></td>
                                    </tr>                
                            </table>
                            </form>

            </body>

Here is my error message enter image description here

Kevin John
  • 99
  • 1
  • 8
  • 2
    because what if the IF Statement evals to FALSE, then $c will not exist and you cannot echo a non-existent variable. – t3chguy Jul 25 '14 at 13:45
  • Initialize it with `$c = null;` before the `if()` block. – Michael Berkowski Jul 25 '14 at 13:46
  • Is that an actual php page (extension ends in .php), running on a server with PHP enabled? – j08691 Jul 25 '14 at 13:46
  • Could you please explain a little more. I couldn't catch your answer.I declared $c here "$c=$a+$b;" . I am working on a local host using Wamp .Thanks – Kevin John Jul 25 '14 at 13:46
  • Yeah but what if the submit_btn isn't set, then those three lines of code never get run. – t3chguy Jul 25 '14 at 13:47
  • @KevinJohn You only initialize it if the `$_POST['submit_btn']` is set. Therefore, the first time the page loads it is _not_ set because the form has not been submitted and `$c` doesn't get a value. Initialize it to null before the `if()`. – Michael Berkowski Jul 25 '14 at 13:47
  • You can use `echo @$a;` to hide any errors if you so wish, highly not recommended. – t3chguy Jul 25 '14 at 13:49
  • 1
    please @t3chguy then don't give them this option. -j08691 i love if someone wants to help but your question was special. – ins0 Jul 25 '14 at 13:50
  • He asked for a way to 'hide' the errors. Do not tell me to not try and help him with what he wants, even if its not a very common practice in PHP. – t3chguy Jul 25 '14 at 13:51
  • if he wants to ``error_reporting(0);`` – ins0 Jul 25 '14 at 13:52
  • 1
    That's even worse than hiding the errors only on one line. – t3chguy Jul 25 '14 at 13:52
  • @Michael Berkowski ,(about Duplicate question issue) I read all the answer but it answers not meet my question. – Kevin John Jul 25 '14 at 13:52
  • 1
    every option to hide a error is equal worse, you don't do it – ins0 Jul 25 '14 at 13:53
  • 1
    @KevinJohn there are three people giving you an almost identical answer, mathematics itself states the chances of us being correct are higher than that of yourself, so how about you believe us; that $_POST['submit_btn'] is undefined until that button is pressed. – t3chguy Jul 25 '14 at 13:53
  • Actually, even go ahead and try it: `` at the top of your php file. – t3chguy Jul 25 '14 at 13:55
  • 3
    @KevinJohn You need to understand that upon first arriving at the page, before submitting the form, `$_POST['submit_btn']` has no value. That's when you see the error. If you proceed to submit the form, it will have a value and `$c` gets defined. The solution is simple - initialize it ahead of time. `$c = null; if (isset($_POST['submit_btn'])) {...// etc...}` – Michael Berkowski Jul 25 '14 at 13:56
  • 1
    @ Michael Berkowski , @t3chguy , I initialised the $c= null. Now its working well. Thanks for the answer everybody. Now my code is – Kevin John Jul 25 '14 at 13:59
  • @ Michael Berkowski , Need to change the title for some other for future reference that you mentioned as it is duplicate. – Kevin John Jul 25 '14 at 14:06

1 Answers1

0

Its because if $_POST['submit_btn'] is not set your $c variable doesn't exist, yet you are using it in the form

Lemuel Botha
  • 659
  • 8
  • 22