I have a full working php script for user activation that I wrote. Fully working in a sense that it displays no errors while on my actual web hosting, although when I am working in WAMP2 I get errors such as these.
Notice: Undefined index: type in C:\wamp\www\activate.php on line 4
Notice: Undefined index: username in C:\wamp\www\activate.php on line 5
Notice: Undefined index: gender in 开发者_如何转开发C:\wamp\www\activate.php on line 6
Notice: Undefined index: formvalue in C:\wamp\www\activate.php on line 29
I know what the problem is, it is because the page activation.php is both for the form submission from create.php (the form on create.php has a hidden value of 1 and if it exists then the form is submitted into the mysql db) and if the form value is not present then the user is activating their account.
When the user is activating, the variables that I have defined using $_POST and the hidden form value of 1 are not defined which is causing the errors.
My question is why are these errors not displayed when my pages are loaded on my actual hosting but are present in WAMP2? And do these errors matter, is it bad practice to have undefined variables? And if so how do I overcome the problem?
Thanks and I hope I explained that all ok.
is it bad practice to have undefined variables?
Yes
how do I overcome the problem?
By not using variables that don't exist.
Or by using variables that have been initialized with a default value, if the expected value has not been provided.
For example :
if (isset($_POST['login'])) {
$login = $_POST['login'];
} else {
$login = '';
}
Or, using the ternary operator, to get something shorter :
$login = isset($_POST['login']) ? $_POST['login'] : '';
why are these errors not displayed when my pages are loaded on my actual hosting but are present in WAMP2?
If can be because are displayed on one server, and not on the other -- see display_errors
.
Or it can be because the error_reporting
level is not the same on the two servers.
Typically, your development server is configured to report notices (see E_NOTICE
), and to display errors -- which makes things easier when it comes to finding the cause of some problems.
Your PHP is configured to show Notices, Warnings etc. on your WAMP2 configuration.
In the hosting environment this is disabled, and will probably only show critical errors.
You can find out more about error reporting : http://forums.devshed.com/php-development-5/changing-error-reporting-in-php-ini-549798.html
If you don't know where your php.ini file is, try to create a php file with the following code to show current configuration:
<?php
phpinfo();
?>
http://php.net/manual/en/errorfunc.configuration.php - display_errors and display_startup_errors
Also what I want to say - its good practice to check if array key exists:
$login = isset($_POST['login']) ? $_POST['login'] : NULL;
Your host has most likely error_reporting set to a value in which E_NOTICE's are not shown (E_ALL ^ E_NOTICE). I always check the existence of my variables, so no notices on undefined variables / indices are thrown. That aids me in debugging, in which I always enable E_NOTICE (error_reporting = E_ALL).
You may want to use the filter_*
methods for getting POST or GET values because it also checks whether a submitted value is a string or not.
精彩评论