Hey guys,
I use a simple statement:
<input type="text" value="<?php echo($_POST['username']); ?>" />
To post the username in a textbox when a user fails to login, so they don’t have to retype their username (only need retype password). This works fine on my MAMP installation (mac os x) as it does not post anything if username is not set, and if it is set, it posts the username.
On WAMP, however, I am getting an error saying it is undefined. I solved this by checking if it exists before trying to echo it:
<?php if(isset($_POST['username'])) echo($_POST['username']); ?>
Why is this behaving differently between WAMP and MAMP? Is it technically correct to check if a variable exists before trying to echo it? Any more efficient way to solve my problem?
Ill start with the simple part first which is the errors, basically depending on how the PHP release was bundled depends on whats set in the php.ini file, by default WAMP comes with E_ALL & ~E_NOTICE turned on because its packaged to emulate the settings on a production server the best it can. It’s 100% important when on a development machine that error reporting is turned on as your script could have issues and you might never know about then unless you check the error_log daily.
As for checking if a variable/index exists my personal opinion is to do this always, i always ensure that the variable/index does exist before i let the script continue not only to avoid errors but as a way to ensure my scripts stay secure from dodgy requests. An easier way i guess to make sure variables and indexes exist is to create a function that parses all the $_POST, $_GET, $_SESSION, $_COOKIE etc super globals then pushes each value into an array called for example $_GETVAR so rather then relying on the issetFONT=Courier New[/FONT] function you call for example…
$_GETVAR = setSuperGlobals();
and the setSuperGlobals() function will parse or has already parsed the request variable so its ready for usage straight way.
Perfect man thanks a lot!!! 