How to write better php code?

Hi friends,
I am just starting php. How to enrich my knowledge in PHP. Please help me to write better php code specially oops concept.

I can only advice three things.

  1. Practice
  2. Practice
  3. Practice

read a lot.

Hi Webiswide,

I’ve some suggestions on how you might approach both topics:

The PHP language
If you want to get a deeper understanding of the language and learn about some best-practices, I’ve found http://www.phptherightway.com/ to be very useful, and it links to other tutorials and sources of good information.

It’s also worth spending some time browsing the PHP manual, to learn about the various built-in functions… oftentimes you can find yourself trying to re-implement some piece of functionality that already exists in the language itself.

OOP Concepts
You can learn about the OOP features of PHP from the manual, but it won’t really help you to learn how to write good OO code - for that, you can learn a great deal from looking at other peoples code. Github is great for this because it makes it easy to browse around the source code of thousands of different projects. Find some well known figures from the PHP community and checkout their github repositories, see what you can learn from their code.

Most importantly, as claro says, is to practice. For many of us, the best way to really ‘get’ a concept is to build something using it. If you’re reading up about PHP’s session handling, write a small app to try out what you’ve learned.

Lastly, browsing forums like this one lets you see the kind of problems people commonly run into and different ways of solving them.

When developing on every project always include this common file ( can safely be used locally or online)

#constants.php


<?php 
  # set $path='your-project';
     ini_set('error_log', $path .'/php_error.php');

  defined('LOCALHOST') ?: define( 'LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME'] ); 

  if (LOCALHOST):
    // following parameters should already be set in your php.ini
    error_reporting(-1); 
    ini_set('display_errors', 1);

    # validate previous settings 
    if( -1 !== error_reporting() * ini_get( 'display_errors' ) ):
      echo 'Webiswide we have a problem :(';
      // phpinfo();
      die;  
    endif;
  else:  
    # ONLINE so do not show errors or warnings, etc
    # error_reporting(1); // maybe, up to you
    ini_set('display_errors', 0);
  endif;

# debugging function
if ( LOCALHOST && (! function_exists('vd') )):
  //===================
  //
  //  usage:
  //        vd( $anyVariable );
  //        vd( $anyVariable, 0 );
  //
  //===================
  function vd($param, $mode=1)
  {
    echo '<pre>';
       if($mode):
          print_r($param);
       else:
          var_dunp($param);
       endif;
    echo '</pre>';
  }
endif;


Always check the value of a function otherwise one day you will get bitten :slight_smile:

Just received this PHP email:
http://www.phpclasses.org/blog/package/1351/post/1-10-Steps-to-properly-do-PHP-Bug-Tracking-and-Fixing-as-Fast-as-possible.html

I would add setting timezone to that script. I think it is required since >= 5.3 and might lead to a lot of errors reported to the logs.

As the great philosopher Buffy Summers once pointed out, that is one thing repeated three times.

It’s actually repeated twice. :wink: