Forum Moderators: coopster
I recently upgraded to php5, And I will be the first to admit that my coding isn't the neatest, and I don't always follow the correct syntax, However in php 5 there seems to be now room for mistake in syntax
Currently I have some session variables that are filled in by post data, when the post data hasn't been sent i get this error. I'm assuming it's because when the post hasn't been sent then the ending variable $login is equal to being empty, or undefined. I can't seem to find a way around this to keep it from being undefined. I know I could turn error reporting off, but that would just be sloppy.
Whats the best way to deal with this:
<?
session_start();
$server = $_SERVER['HTTP_HOST'];
$_SESSION['loggedin'] = $_POST['login'];
$loggedin = $_SESSION['loggedin'];
?>
added, I hope you don't mind that I put this here instead of making a new thread, don't want to spam the forum.
I'm using str replace to switch
with <div class="code"> and with </div> However doing it this way allows for them to not have the closing div and screw up the site layout by doing so, whats the best way to check to make sure that both tags are there? If both aren't there I want it to show as instead of creating the div, Doing a replace [code]% doesn't work I guess because the % represents one character Any help would be great.
if (isset($_POST['login']))
{
// Form has been submitted safe to use $_POST super global
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$var3 = $_POST['var3'];
} else {
// Form hasn't been submitted, use code not related to processing data from the $_POST super global
}
You won't need to test for the existance of each $_POST variable ... just for one from the form that you know you are expecting. It's always good practice though, to test for the existance of something before you use it... saves heartache later.
It seems like such a waste
Au contraire. What's a waste is code that does not function properly because you have a small error that you haven't noticed because you've turned off error reoporting for notices.
I was going through some code not too long ago that I was trying to adapt for my purposes and the author had things like
$var['index1']
$var['indxe1']
Because he wasn't developing with error_reporting set to show notices, he didn't realize he had made a typo and that
if ($var['indxe1'])
would always and under all circumstances evaluate to false. His condition would never be met.
Check all vars. Assign default values. It will save you far more time in the long run than you would save by not declaring default values and checking for undefined variables and just getting by with turning notices off.