Coding style
Posted: Mon Mar 20, 2006 5:16 am
I have a question about coding style. During development, I set error reporting to strict. I also use the method of declaring empty arrays for $clean, $html and $mysql. Now, let's say I have the following:
However, if $_POST['username'] is not set, the code returns notices about undefined variables. I can place isset() conditions everywhere like in the code below, but I was wondering: are there other, better ways? Or, because I probably will turn off error reporting in a live situation, those notices will not show up, I shouldn't bother writing all those if(isset()) checks? (don't like that)
I would certainly like to hear from others how they deal with something like this. I like my code to be as clean and readable as possible, so any tips on this are welcome.
Code: Select all
<?php
// set error reporting to strict
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
// filter input
$clean = array();
if (ctype_alnum($_POST['username']))
{
$clean['username'] = $_POST['username'];
}
//escape output to html
$html = array();
$html['username'] = htmlentities($clean['username'],ENT_QUOTES, 'UTF-8');
echo "<p>Welcome back, {$html['username']}.</p>";
?>Code: Select all
<?php
// set error reporting to strict
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
// filter input
$clean = array();
if (isset($_POST['username']) && ctype_alnum($_POST['username']))
{
$clean['username'] = $_POST['username'];
}
//escape output to html
$html = array();
if(isset($clean['username']))
{
$html['username'] = htmlentities($clean['username'],ENT_QUOTES, 'UTF-8');
echo "<p>Welcome back, {$html['username']}.</p>";
}
?>