AD MANAGEMENT

Collapse

BEHOSTED

Collapse

GOOGLE

Collapse

How To : Improve Your PHP Programming

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How To : Improve Your PHP Programming

    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:
    <?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.

    http://img76.imageshack.us/img76/6450/sc2but4ng.gif | http://img495.imageshack.us/img495/7...gorwtan0je.gif | <a href="http://webtools.sc2.info"><img src="http://img129.imageshack.us/img129/8682/sc2webtan7ju.gif" border="1" width="100" alt="Free Webtools for all" /></a> | <a href="http://www.indexrated.com"><img src="http://img398.imageshack.us/img398/4813/listed1pb.gif" border="1" width="100" alt="Index Rated Directory - Rating Quality Sites" /></a>

    Get all Games,Apps and Wallpapers Nokia, Samsung and Sony Erricson!!

Unconfigured Ad Widget

Collapse

Announcement

Collapse
1 of 2 < >

FreeHostForum Rules and Guidelines

Webmaster forum - Web Hosting Forum,Domain Name Forum, Web Design Forum, Travel Forum,World Forum, VPS Forum, Reseller Hosting Forum, Free Hosting Forum

Signature

Board-wide Policies:

Do not post links (ads) in posts or threads in non advertising forums.

Forum Rules
Posts are to be made in the relevant forum. Users are asked to read the forum descriptions before posting.

Members should post in a way that is respectful of other users. Flaming or abusing users in any way will not be tolerated and will lead to a warning or will be banned.

Members are asked to respect the copyright of other users, sites, media, etc.

Spam is not tolerated here in most circumstances. Users posting spam will be banned. The words and links will be censored.

The moderating, support and other teams reserve the right to edit or remove any post at any time. The determination of what is construed as indecent, vulgar, spam, etc. as noted in these points is up to Team Members and not users.

Any text links or images contain popups will be removed or changed.

Signatures
Signatures may contain up to four lines

Text in signatures is subject to the same conditions as posts with respect decency, warez, emoticons, etc.

Font sizes above 3 are not allowed

Links are permitted in signatures. Such links may be made to non-Freehostforum material, commercial ventures, etc. Links are included within the text and image limits above. Links to offensive sites may be subject to removal.

You are allowed ONLY ONE picture(banner) upto 120 pixels in width and 60 pixels in height with a maximum 30kB filesize.

In combination with a banner/picture you can have ONLY ONE LINE text link.


Advertising
Webmaster related advertising is allowed in Webmaster Marketplace section only. Free of charge.

Shopping related (tangible goods) advertising is allowed in Buy Sell Trade section only. Free of charge.

No advertising allowed except paid stickies in other sections.

Please make sure that your post is relevant.


More to come soon....
2 of 2 < >

Advertise at FreeHostForum

We offer competitive rates and a many kinds of advertising opportunities for both small and large scale campaigns.More and more webmasters find advertising at FreeHostForum.com is a useful way to promote their sites and services. That is why we now have many long-term advertisers.

At here, we also want to thank you all for your support.

For more details:
http://www.freehostforum.com/threads...eHostForum-com

More ad spots:
http://www.freehostforum.com/forums/...-FreeHostForum
See more
See less
Working...
X