Results 1 to 7 of 7

Thread: How To : Improve Your PHP Programming

  1. #1
    Join Date
    Jan 2008
    Location
    gods own country
    Posts
    2,319

    Exclamation How To : Improve Your PHP Programming

    How To : Improve Your PHP Programming
    Hello everyone,

    I've decided that I would make a thread here describing the different things that I do to improve other people's PHP scripts.

    Hope you enjoy it and learn a thing or two.

    1 - Your PHP Tags
    I know some of you prefer to use the short tags when writing PHP scripts <? ?> but this is not always the best way of doing it.
    The standard tags <?php ?> are much better as they will work on every server that you write your PHP code on. You may move to a server some day that doesn't allow the short tags or the ASP-style tags and you will have to sit for an hour and update your PHP scripts.

    2 - Debugging Your PHP Code
    Some of us may run into a problem when programming a PHP script and don't know what's wrong with it. The error_reporting() function in PHP helps you out by telling every error you have on your page. To show all of the errors on the page that you're editing, put this on the second line :
    PHP Code:
    error_reporting(E_ALL);


    3 - Debugging Your PHP Code (again)
    When you finish editing your 1200-line PHP script, click onto it in your Internet browser, you see an error that says that is on line 561. Don't hit the panic-attack button quite yet, because there is an easy way to find out what line 561 is. Follow these easy steps :
    - Open up Microsoft Notepad
    - Paste your PHP script into it
    - Go to 'Edit' >> 'Go To...' (or Control+G)
    - Type in line #561 and hit the enter key
    - Your cursor is taken to line #561.
    - Look above and below line #561 to see if there is any kind of trouble.
    - Fix the error, re-upload the script to your website, and most likely it will work. If there is another error, repeat the above steps.

    4 - Using Comments
    If you have a 1200-line PHP script, it may be quite hard to figure out what's going on all through-out it. The solution to figure out what you're doing is to add PHP-comments.
    PHP-comments are different than the <!-- HTML Comments --> as they are not outputted to the user's page (meaning that they are not even going to see it in the source code).
    There are three ways to make comments in PHP :
    PHP Code:
    <?php
    // The double-backslash is my personal favorite. I add another set after my code so that it looks even, though it is not necessary. //
    # The hash-style comment is another way of making a comment.
    /* And, this is the final way of making PHP-comments. You can use
    multiple
    lines
    at a time by using this style. */
    ?>

    You can decorate it however you like, you are the only one who may use them.

    5 - Indenting Your PHP Codes
    I don't personally like to indent my PHP codes, but it helps when reading it. When I do have to, I use the tab key to accomplish this. Example :
    PHP Code:
    <?php
    // Settings //
    $var1 = "This";

    // Showing Variables //
    if($var1 == "This"){
    echo
    "You said This";
    }else{
    echo
    "You said That";
    }
    ?>

    6 - Improving your PHP-File Includes
    I'm sure that most of us on here include a PHP file or two for our layouts. Well, what if your layout file was missing ? Wouldn't that look pretty unprofessional to the people on your website ?
    In every PHP-script that I write, I make sure that the file exists before it is even included. Here's an example :
    PHP Code:
    <?php
    if(!file_exists("layout.inc.php")){exit("Error : LayOut File Missing");}else{include_once("layout.inc.php");}
    ?>

    I'm sure that a small error message will seem better than half a page that is all messed-up looking.

    7 - Your MySQL Queries
    Sometimes when you're writing a PHP script that includes connections to your MySQL database, you may run into a few problems. Most everyone that had MySQL problems ran a command like this one :
    PHP Code:
    <?php
    mysql_query
    ("INSERT INTO tableName ('id','name') VALUES('1','Mike')");
    ?>

    ..and they figure out that it's not inserting into their database. Here's the solution to this :
    PHP Code:
    <?php
    mysql_query
    ("INSERT INTO tableName ('id','name') VALUES('1','Mike')") or exit("MySQL Error : " . mysql_error());
    ?>

    8 - Combining Alike If-Then Statements
    You may have a register page, and want to make sure that everything has been filled-in. You may use many if-then statements like so :
    PHP Code:
    <?php
    if(!$_POST[name]){exit("Sorry, but you did not fill-in all of the requested fields.");}
    if(!
    $_POST[email]){exit("Sorry, but you did not fill-in all of the requested fields.");}
    ?>

    You can combine these two lines into one by joining their if-then statements together :
    PHP Code:
    <?php
    if((!$_POST[name]) || (!$_POST[email])){exit("Sorry, but you did not fill-in all of the requested fields.");}
    ?>

    Simply, || is the same thing as OR and && is the same as AND.

    9 - Using echo or print ?
    Most of you may say 'echo is the same thing as print', in which I agree with you all. The echo command is much faster than the print command, and is one less character to type. The echo command came later than the print command (I believe), so you make the judement on which to use.

    10 - Printing out a Huge Chunk of HTML at a Time
    Well, I'm sure that many of us found a way to get around this, but I'd like to share with you a few of the ways you can do it.

    1 - Break off your PHP-code, print the HTML, and start your PHP-code up again. (I don't prefer doing this as it looks pretty unprofessional to me).
    2 - Adding backslashes to each HTML tag. (It works, but takes forever to do).
    3 - Using the echo/print command, but without having to do much work. (I recommend) :
    PHP Code:
    <?php
    // Showing a huge chunk of HTML at a time //
    echo<<<END
    <font face="Verdana" color="Orange" size="3">Large, Orange Text in Font Size 3</font>
    <br><br>
    More HTML down here..
    <br><br>
    <div align="Center">Centered text</div>
    END;?>

    Well, I have many other things to tell about sprucing up your PHP-code, but I don't want to bore you.

    I hope I've helped.

    Best Regards,
    - sanils.

  2. #2
    Join Date
    Sep 2008
    Posts
    5

    Default

    ill try that

  3. #3
    Join Date
    Sep 2008
    Posts
    4

    Default thx

    thanks guy , i will try that

  4. #4
    Join Date
    Sep 2008
    Posts
    6

    Default nice tips...

    These are good tips to improve php coding... Having a good coding convention will help especially in debugging..

  5. #5
    Join Date
    Feb 2009
    Posts
    5

    Default

    thanks a lot

  6. #6
    Join Date
    Feb 2009
    Posts
    7

    Default

    Ild just like to point out something on the echo and print argument. You say "The echo command is much faster than the print command". I'ld like to point out why.

    Print has a return value, echo does not.


    Consider:

    PHP Code:
     <?php 

    if (print('foo')) { print('foo printed'); } // Will be true 

    if (echo('foo')) { echo('foo printed'); } // Will be false 

    ?>
    Evidently print will always return logical true (1), unless there is a serious error with PHP and the string does not print. Obviously PHP uses C prototypes but I will convert them into PHP so we can see what is going on.

    PHP Code:
     <?php 

    function print($stringToPrint) { 
      
    system.out($stringToPrint); 
      return 
    1


    function echo (
    $stringToPrint) { 
      
    system.out($stringToPrint); 


    ?>
    Hope that helps clear that up.

    ________________
    credit counseling cheapest loans

  7. #7
    Join Date
    Mar 2009
    Posts
    6

    Default ...

    wow thanks so much, this helped me a lotttt, liek i was so clueless before and now i know a lot mroe, thanks again, (:

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •