Page 1 of 1

Coding style

Posted: Mon Mar 20, 2006 5:16 am
by matthijs
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:

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>";
?>
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)

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>";
  }
?>
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.

Posted: Mon Mar 20, 2006 5:21 am
by duk
well if you dont like isset, you can use "@", i dont like to, this thing of isset(), this procedure of using isset to supreme this errors doesn't look to me a good programme pratice, but everyone uses and in most cases to supreme this error.

Posted: Mon Mar 20, 2006 10:16 am
by Chris Corbyn
Use isset() and/or empty(). It's good practice. Code that's aimed at production should not contain notices neither. E_STRICT causes some problems between PHP4 and 5 however. I work to E_ALL.

Posted: Mon Mar 20, 2006 10:19 am
by John Cartwright
E_STRICT causes some problems between PHP4 and 5 however. I work to E_ALL.
I don't think many programmers code on E_STRICT, especially those "var" enthusiasts :wink:

Posted: Mon Mar 20, 2006 10:41 am
by matthijs
Ok, thanks for the input guys. Appreciated.

I probably stick to the style in the second code example I gave. I'll use the isset() and empty() then. I'll code for E_strict. The reason I asked is because the code is getting cluttered really fast with all those conditions/comparisons. Easy to let a mistake slip in, especially considering the sometimes very subtle but crucial differences in variable evaluation. But I guess with experience that'll be easier.
especially those "var" enthusiasts Wink
Who are they?

Posted: Mon Mar 20, 2006 10:44 am
by John Cartwright
Who are they?
Well.. for some odd reason

Code: Select all

class className 
{
   var $blah;
}
is now deprecated since php5, but technically it is not incorrect -- so some people still use it. Although, var is returning in php6
8O

Posted: Mon Mar 20, 2006 10:56 am
by feyd
I code with strict on. :P

Posted: Mon Mar 20, 2006 11:16 am
by matthijs
Jcart wrote:
Who are they?
Well.. for some odd reason

Code: Select all

class className 
{
   var $blah;
}
is now deprecated since php5, but technically it is not incorrect -- so some people still use it. Although, var is returning in php6
8O
Ah, I see. I remember testing a class with E_strict and getting all those notices. So that's why.
feyd wrote:I code with strict on. Razz
Yes, but that's not fair.
2. Feyd doesn't make parse errors, the PHP parser makes errors when parsing feyds (correct) code.
24. When Feyd looks at code he can actually see The Matrix...
:D