Activity Stream
48,167 MEMBERS
61010 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Page 1 of 3 123 LastLast
Results 1 to 10 of 21
  1.     
    #1
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com

    Default 10 Ways to improve your PHP

    Ok heres a list of ways to improve your PHP Programming..

    1. Use the cheat sheets
    When ever your programming in php and mysql you should always have a cheet sheet at hand!,

    there are several out there for you to use as references here are just some







    2. Know your comparison operators


    You should make sure you know your comparison operators and should study them wisely, here are some links that will give you information on where to get to know!







    3. Short cut the else stament
    Speed in programming is a big thing because you down want your user waiting several seconds for your page to load because as they browse your site the seconds they have waited overall will have a downside to your site.

    Take this example of a normal IF / ELSE statement.

    PHP Code: 
    if( expression ){
    $x 10;
    }else{
    $x 5;

    Now this looks like regular coding but by doing the following will remove the need for the else
    PHP Code: 
    $x 5;
    if( 
    expression ){
    $x 10;

    by setting the value of $x before the if statement, when the expression is run if it fails it will just stay at the default value of 5

    4. Less {brackets}
    When using statements such is the if statement theres no need to have the brackets the command will still execute as normal, this decreases the processing time of the statement and also decreases the size of your script

    Example with the brackets
    PHP Code: 
    if($i == 10){
    $i++;

    Same statement without brackets.
    PHP Code: 
    if($i == 10$i++; 
    You can also use multiple if/else staments using the structure. for example
    PHP Code: 
    if ($i == 10$i++; else $i--; 
     
    if (
    $theScene != 'dead') echo 'w00t';
     
    foreach (
    $girls as $girl)
    echo 
    'Heya ' $girl ' Fancy a drink?'
    This method is used alot within the programming industry but Personally i think it makes your code look less attractive, but if you have the need for speed then use this method.

    5. User Ternary Operators
    Ternary operators apply to if/else statements and is usually called "Short Hand" its kinda similar to No.4 "Less {Brackets}" act slightly like a function where it will return data into a variable if provided... examples of ternary operators follow.


    6. Unset your variables
    Unsetting you variabls will free memory in your php allowing other actions to run faster, you unset a variable/array usining the unset() function.. example follows.

    PHP Code: 
    $myName 'Litewarez';
     
    echo 
    $myname;
     
    //Ok so now $myName is not needed throughtout the rest of the script so ill unset it
    unset($myname); 
    Note:
    if you have a globalized variable in a function when you unset the variable it will only unset the local instance of that variable.. example below.
    PHP Code: 
     
    $myname 
    'Litewarez';
     
    function 
    myName(){
    global 
    $myName;
    unset(
    $myName);
    }
    myName();
    echo 
    $myName
    it will still echo Litewarez as only the variable within the function is destroyed but also remember to unset your variables inside functions aswell.

    7. Using catching.
    on every script you should use catching/buffering to speed up your webstie speed, what catching does is create an internal catch of the current page and compresses it before senduing it to the web-browser.. meaning less data to be sent.

    Also i prefer to use ob_start('ob_gzhandler'); instead of ob_start

    what the gxhandler does is using gzip to compress the website data if the web-browser accepts gzip-deflate meaning that all web browsers are supported. example below...

    PHP Code: 
    //you need to call this function before you are going to be outputting data.. such as echo / print / var_dump etc
    ob_start('ob_gzhandler');
     
    //No we can start doing stuff within outr script
     
    if($Litewarez == 'Good Programmer'){
         for(
    $i=0;$i<5;$i++){
              echo 
    'w00t\n';
         }

    //now the out put will be compressed and upto 5 times faster

    8. Using Concatenation.

    When you are joinig variables and strings together the method most people use is

    PHP Code: 
    $User_Welcome "Hello $user"
    No the problem with this is that PHP has to guess the type of operation your trying to perform and this causes slow speeds, by using concatenation php knows what your are trying to gain and then performs the selected task instantly.

    Here are some examples of concatenation!

    PHP Code: 
     
    <?php 
     
    $Welcome_Start 
    'Welcome';
    $Username 'Litewarez';
    $Welcome_End 'to your profile';
     
    /*
    This is the WRONG way to join them strings together!!!!
    */
     
    $Welcome "$Welcome_Start $Username $Welcome_End";
    //OUTPUT: Welcome Litewarez to your profile
     
    /*
    This is the CORRECT way
    */
     
    $Welcome =  $Welcome_Start.' '.$Username.' '.$Welcome_End;
    //OUTPUT: Welcome Litewarez to your profile
    ?>
    all you need to know is joining a variable with a string ( "string" . $var ) is faster


    9. Using Incremental Operators.
    Now these are some operators that are widely used when dealing with numbers, but im going to show you the importance of POST / PRE incremental operators.

    heres the 4 variations of these types of mathematical operators

    • Increment
      • $number++
      • ++$number
    • Decrement
      • $number--
      • --$number
    the difference is that the POST (++ or --) comes after the variable and the PRE (++ or --) comes before.

    the reason for this is the fact that if you use the POST method then the nuber will be incremented AFTER it has been return and if you use PRE method then the number will be incremented then return.. some examples below

    PHP Code: 
    <?php 
    $number 
    5;
     
    //POST
    echo 'I should be 5 : '.$number'<br />';
    echo 
    'I should be 5 : '.$number++. '<br />'// here iv'e started to increment it
    echo 'I should be 6 : '.$number'<br />';
    echo 
    'I should be 6 : '.$number--. '<br />';
    echo 
    'I should be 5 : '.$number--. '<br />';
     
    echo 
    '<br />';
    $number 5;
    //PRE
    echo 'I should be 5 : '.$number'<br />';
    echo 
    'I should be 6 : '.++$number'<br />';
    echo 
    'I should be 7 : '.++$number'<br />';
    echo 
    'I should be 6 : '.--$number'<br />';
    echo 
    'I should be 6 : '.$number'<br />';
    ?>
    now as you can see that when using the ++ before the variable the maths is instance so to speak but when you are using ++ after the variable the changes will not be see until the next time the variable is called.

    10. Error Supressions

    Ok we NEED to know about error supression when using functions.



    if you have built a website and everything is going well and then you start seing an error on your site, well you dont want users to see this error because:
    1. Looks Nasty
    2. Messes up your design
    3. * helps hackers gain information about your directory structure
    Now we dont want any of that now do we? so im going to show you how to supress errors when your programming.

    Theres a few ways you can do this:
    In your php.ini file you can take control of the error_repoerting settings and turn of all errors on your website

    this is what the settings look like in your php.ini file and you can change these accordingly!

    Code: 
    error_reporting = E_ALL & E_NOTICE & E_STRICT


    Possible Variations.
    • E_ERROR => 'error'
      E_WARNING => 'warning'
      E_PARSE => 'parsing error'
      E_NOTICE => 'notice'
      E_CORE_ERROR => 'core error'
      E_CORE_WARNING => 'core warning'
      E_COMPILE_ERROR => 'compile error'
      E_COMPILE_WARNING => 'compile warning'
      E_USER_ERROR => 'user error'
      E_USER_WARNING => 'user warning'
      E_USER_NOTICE => 'user notice'
    Now you can read up about these error constants by reading here
    http://uk3.php.net/manual/en/functio...-reporting.php

    Also the necxt way to change the error types is by using a function in you php script calle error_reporting().

    heres an example of the usage

    // Turn off all error reporting
    error_reporting(0);

    // Report simple running errors
    error_reporting(E_ERROR | E_WARNING | E_PARSE);

    // Reporting E_NOTICE can be good too (to report uninitialized
    // variables or catch variable name misspellings ...)
    error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

    // Report all errors except E_NOTICE
    // This is the default value set in php.ini
    error_reporting(E_ALL ^ E_NOTICE);

    // Report all PHP errors (see changelog)
    error_reporting(E_ALL);

    // Report all PHP errors
    error_reporting(-1);

    // Same as error_reporting(E_ALL);
    ini_set('error_reporting', E_ALL);


    and the next way to do this is by adding the supresser to your functions witch will be the at symbol (@) before the Function so heres an example

    PHP Code: 
    //Working Function
    $data = @file_get_contents('http://www.somesite.com');
     
    //Function that will cause an error
     
    $data = @file_get_contents(1,4,'hello',helloooo); 
    oki so without the @ sysbols the second function will; have couse your script to stop executing with an invlaid parameters error

    so just take into mind that its best to set your php.ini other than using the at sysbol becuase if your debugging you site you dont want to go threw your script removing the @ sysbol from everywhere..

    11. End-Credits

    theres not really much to put in here apart from i hope you enjoy learning some PHP and comment like mad if you like.

    My name: Litewarez, aka Cornetofreak

    NOTE: yuou may copy this post from http:www.besthostingforums.com/ and post it where ever you feel like but please give credits to the original contributer.
    litewarez Reviewed by litewarez on . 10 Ways to improve your PHP Ok heres a list of ways to improve your PHP Programming.. 1. Use the cheat sheets When ever your programming in php and mysql you should always have a cheet sheet at hand!, there are several out there for you to use as references here are just some Rating: 5
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  2.   Sponsored Links

  3.     
    #2
    The Wise One
    Website's:
    twilight.ws ddlrank.com
    Great post litewarez, downloaded the cheat sheets right away! If we had a rep system I would rep you
    I can always be contacted by sending a tweet @twilightws

  4.     
    #3
    Member
    Website's:
    WareztheDDL.com GTFO.ws
    yeh thanks dude


  5.     
    #4
    Respected Developer
    Website's:
    PlatinumW.org NexusDDL.com HD-United.org CheckLinks.org FLVD.org
    Awesome cheat sheets man.

  6.     
    #5
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    no probs doods
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  7.     
    #6
    Member
    nice tips.

  8.     
    #7
    Respected Developer
    Nice tips but #4 is a mytth. Unless you make yourself a 10MB PHP file it won't matter.

  9.     
    #8
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    its not a myth im pretty sure that php processes while / if statments without the braces before trying to parse with the braces so in thoery your right it would not make a huge difference unless you had a large script but consider the amount of if staments that are carried out within a website INCLUDING the ones in loops and then it should make a visible difference to the benchmark reasults..
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  10.     
    #9
    ლ(ಠ益ಠლ)
    Website's:
    extremecoderz.com
    thanks man. nice little reference cards.

  11.     
    #10
    Member
    Awesome cheat sheets man.


Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Improve your site performance, improve SEO
    By NewEraCracker in forum Tutorials and Guides
    Replies: 41
    Last Post: 21st Jan 2012, 01:51 PM
  2. Help us improve
    By happyface4ever in forum Webmaster Discussion
    Replies: 2
    Last Post: 3rd Feb 2011, 06:10 PM
  3. PLz Help Us to Improve and how
    By ahsanmcsd in forum Site Reviews
    Replies: 4
    Last Post: 6th Nov 2010, 01:33 PM
  4. Best ways to improve Google PR?
    By Storming in forum Webmaster Discussion
    Replies: 12
    Last Post: 6th Jan 2010, 09:55 AM

Tags for this Thread

BE SOCIAL