6

On a remote server there is no problem, but in localhost (xampp 3.1.) I cannot turn off reporting notices.

<?php
$Fname = $_POST["Fname"];
...

result:

Notice: Undefined index: Fname in D:\xampp\htdocs\xx\php01\form01.php on line 6

php.ini

; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
; http://php.net/error-reporting
error_reporting = E_ALL & ~E_NOTICE  //shouldn't this line turn off notice reporting ?

Any suggestion ?

ficuscr
  • 6,975
  • 2
  • 32
  • 52
qadenza
  • 9,025
  • 18
  • 73
  • 126
  • 8
    Why would you want to turn off notices for development? Personally in Dev environment, I believe you should use `E_ALL` and that is it. Have you checked that you are not changing this value at run time? – Mike Brant Nov 12 '13 at 19:20
  • 3
    The best course of action is to fix the errors. Notices _are_ errors, but ones the program can recover from. `$Fname = isset($_POST['Fname']) ? $_POST['Fname'] : '';` – Michael Berkowski Nov 12 '13 at 19:22
  • Suppressing notices is a little bit like hanging wallpaper over crumbling plaster walls. – Michael Berkowski Nov 12 '13 at 19:24
  • 2
    @MikeBrant, it is long story about my experimenting with php. I simply want to know how to exclude them. – qadenza Nov 12 '13 at 19:24
  • To add to what @MichaelBerkowski said and to give you specific context to the error message you are getting. You have a case in your code where you expect an array index to be present and it isn't. This should strike you as a potentially HUGE problem as your application is not behaving as you expect it to. If you expect that this index may not be present, then you should utilize `isset()` or `empty()` or `array_key_exists()` or similar before trying to operate on this array index. – Mike Brant Nov 12 '13 at 19:24
  • 1
    I typically keep `E_ALL` setting in prod as well. I just make sure the errors are going to log only and not displaying to screen. Hiding notices, strict warnings, etc. is just bad programming practice. – Mike Brant Nov 12 '13 at 19:28
  • @MikeBrant, please check this `http://www.tizag.com/phpT/examples/formfinale.php. I just want to reproduce this form on my localhost. – qadenza Nov 12 '13 at 19:31
  • @SunSky OK. But what does that have to do with this question? Code examples like that are for BASIC demonstration purposes only and in no way imply good coding practice, usually to keep the example short. In a really world application, one would need to verify that values are actually set. – Mike Brant Nov 12 '13 at 19:32
  • @MikeBrant `E_ALL` in production environment tends to make REALLY BIG logs. Ofc it depends of the website. But most CMS are really notice & warnings verbose. – OlivierH Nov 12 '13 at 19:32
  • @SunSky I just had a look at that website and I wouldn't recommend using it anymore! The codes are not indented and the MySQL section still uses the old `MySQL` extension instead of the better `MySQLi` and `PDO` extensions. – ComFreek Nov 12 '13 at 19:33
  • @OlivierH How does `E_ALL` make bigger logs in Prod than in Dev (outside the typically higher number of requests in Prod of course)? If you fix these errors in Dev, they should never reach Prod. If you have huge numbers of warnings/notices in Prod, you have bad code - plain and simple. – Mike Brant Nov 12 '13 at 19:35
  • @OlivierH E_ALL in production only makes big logs if the application code is sloppy. No code should ever produce _any_ errors, ideally. Develop with E_ALL (and E_STRICT!), eliminate _all_ errors regardless of severity and your corresponding production log will be empty of errors as well. – Michael Berkowski Nov 12 '13 at 19:38
  • @SunSky After adding this line to php.ini `error_reporting = E_ALL & ~E_NOTICE` did you also restart `apache server` ? – Noor Nov 12 '13 at 19:38
  • @Noor, yes I restarted it. – qadenza Nov 12 '13 at 19:38
  • @MikeBrant I agree, I just said this is not always possible to correct all notice and warning errors. When you got code with complex business logic, you can't test all possibilities during development/testing phases. I don't talk about most CMS, like Drupal : you can't correct all the php notices it outputs. And with thousands of visitors, you got huge logs file. – OlivierH Nov 12 '13 at 19:44
  • @OlivierH Fair enough. When you have to deal with some third party code that is a consideration to make. When writing your own application, however, it is a bad idea to do this. Since OP is new to PHP (and like programming in general), I just wanted to strongly emphasize this. – Mike Brant Nov 12 '13 at 19:47
  • 2
    How about the `display_errors` in `php.ini` file ? is this `On` or `Off` ? – Noor Nov 12 '13 at 20:16
  • 1
    @Noor, `display_errors = On` – qadenza Nov 12 '13 at 20:22
  • 2
    Change it to `display_errors=Off` and see. – Noor Nov 12 '13 at 20:23
  • @Noor, AND OliverH: My Appache was configured to start up automatically with xampp. I treid now to turn off this option and turn on apache manually, clicking on the button `start` and now everythin works fine. So it seems solved, but it's not fully understand WHY? – qadenza Nov 12 '13 at 20:33
  • @SunSky How did you reload the apache service ? By closing XAMP ? I'm not sure closing xamp makes apache service to be stopped, it could be an explanation. – OlivierH Nov 12 '13 at 20:38
  • @OlivierH, yes I fully closed xampp and apened it again. I suppose that if appache is configured to start automatically that it takes the the php.ini from some kind of its cache memory or something... I tried now everything again. manually started appache - everything works and automatically - remembers the last php.ini options. Maybe you should try also – qadenza Nov 12 '13 at 20:42
  • @OlivierH, sorry, I just now see the idea of your question. Yes, I restarted xampp. Is it not enough for reload appache ? – qadenza Nov 12 '13 at 20:48
  • @Noor, and OliverH thanks A LOT for your time and your efforts ! Solved – qadenza Nov 12 '13 at 20:53

5 Answers5

14

Write this code in start of your file.

ini_set('display_errors', 0);
error_reporting(E_ERROR | E_WARNING | E_PARSE); 

Edit1

If you did not want to use above lines then you have to write @ before $_POST to suppress notices, like

$Fname = @$_POST["Fname"];

Edit 2

With this line error_reporting = E_ALL & ~E_NOTICE also change display_errors = Off although its bad programming practices.

Noor
  • 1,351
  • 8
  • 27
  • Noor, thanks, it works, but in that case I must write these lines on each file. Why it is not possible to do on php.ini level ? – qadenza Nov 12 '13 at 19:47
  • 2
    Your `php.ini` file with this code `error_reporting = E_ALL & ~E_NOTICE` should absolutely does the trick. Not sure why its not working with you. Are you using any framework/CMS may be there `error_reporting` is set again to true – Noor Nov 12 '13 at 19:52
  • 1
    Noor, no framework, no cms. But if you don't see any error in my php.ini line - let's say solved. Who knows what could be the real reason. Thanks, anymore. – qadenza Nov 12 '13 at 19:56
4

If your running XAMPP and want to turn off notices (or other features):
1. Make sure your editing the right INI file (select config from the control panel)
2. Turn display_errors=on
3. Turn error_reporting=E_ALL & ~E_NOTICE (This will only suppress notice errors)
4. Important - Make sure XAMPP is not overriding your settings further down the file (read the notice above the first set of settings)
5. Stop and Start Apache after saving the file

MikeR
  • 41
  • 1
1

Try to do a phpinfo(); just before your $Fname = $_POST["Fname"]; line. What's the error_reporting property value ? See this or this to understand the value displayed in the table.

If it's not what you expected, check that the property is not changed by php. It's also possible you edited the wrong php.ini file : XAMPP tends to copy the original php.ini file and create his own. Use phpinfo();, search for 'php.ini' string in the table : you will find the path of the php.ini file used.

Last thing, maybe the problem is that you did not properly restarted apache after you changed php.ini file. The thing is that xamp is a distinct process of the apache service. Closing XAMP do not make apache service to be stopped, you better use XAMPP Control Panel to stop/start apache.

Community
  • 1
  • 1
OlivierH
  • 3,875
  • 1
  • 19
  • 32
  • OliverH, I got all the phpinfo tables and at the end - `Notice: Undefined index: Fname in D:\xampp\htdocs\xx\php01\form01.php on line 7` – qadenza Nov 12 '13 at 19:52
  • Yes, but what you got in the phpinfo table ? I mean search for error_reporting string, and give us the value displayed. – OlivierH Nov 12 '13 at 19:53
  • OliverH, excuse me, how can I search for that string. Ctrl F in fiefox and what to search ? – qadenza Nov 12 '13 at 19:58
  • Something like 'error', 'reporting', 'error_reporting', 'error reporting'... You should find it fast. – OlivierH Nov 12 '13 at 20:02
  • OiverH I found error-reporting and in the next (right) cell this number - `32767`. Also in the second (right) cell – qadenza Nov 12 '13 at 20:04
  • As you can see here http://php.net/manual/en/errorfunc.constants.php, 32767 is E_ALL. You modified the wrong php.ini IMO. I edited my answer. – OlivierH Nov 12 '13 at 20:09
  • OliverH, but I opened the php.ini via xampp - config - php.ini. Just a moment, pls, I'll find that file by standard file manager – qadenza Nov 12 '13 at 20:11
  • OliverH, I found php.ini with php.info. `Loaded Configuration File - D:\xampp\php\php.ini` and opened that file in win explorer, and search for `error-reporting` and found - `error_reporting = E_ALL & ~E_NOTICE` – qadenza Nov 12 '13 at 20:19
  • Try to put `E_ALL ^ E_NOTICE` instead, and then reload your apache. – OlivierH Nov 12 '13 at 20:22
1

The most simple way to solve this problem is as follows: 1. Turn off Apache server 2. Go to c:\xampp\php\ 3. Rename php.ini to php.ini.bak 4. Rename php.ini-production to php.ini 5. Turn on the server again.

It has a flaw that it reverts everything to production mode. but you can always undo this.

1

Remember to uncomment error_reporting in php.ini by removing the ';' before 'error_reporting'.

It really wasted my time here

Ebube
  • 475
  • 4
  • 5