Coding style

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Coding style

Post 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.
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I code with strict on. :P
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post 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
Post Reply