Ways to improve your PHP

Discussion in 'Archives' started by EXT, Apr 7, 2010.

Ways to improve your PHP
  1. Unread #1 - Apr 7, 2010 at 9:46 AM
  2. EXT
    Joined:
    Apr 23, 2008
    Posts:
    117
    Referrals:
    1
    Sythe Gold:
    0

    EXT Active Member
    Banned

    Ways to improve your PHP

    Okay here's a tutorial on 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:
    • MySql
    http://www.addedbytes.com/cheat-sheets/download/mysql-cheat-sheet-v1.png
    • PHP
    http://www.addedbytes.com/cheat-sheets/download/php-cheat-sheet-v1.png
    • Regex
    http://www.addedbytes.com/download/regular-expressions-cheat-sheet-v2/png/
    • Mod Rewrite
    http://www.addedbytes.com/cheat-sheets/download/mod_rewrite-cheat-sheet-v2.png
    • CSS
    http://www.addedbytes.com/cheat-sheets/download/css-cheat-sheet-v2.png
    • HTML
    http://www.addedbytes.com/cheat-sheets/download/html-cheat-sheet-v1.png

    2. Know your Comparison operators.
    You always have to 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 them.

    • Main information source
    http://docs.php.net/manual/en/language.operators.comparison.php

    3. Short cut the ELSE statement.
    The speed in programming is a huge requirement.

    Take this example of a normal IF / ELSE statement.
    PHP:
    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:
    $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:
    if($i == 10){
    $i++;
    }
    Same statement without brackets.
    PHP:
    if($i == 10$i++;
    You can also use multiple if/else staments using the structure. for example:
    PHP:
    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:
    $myName 'EXT';
     
    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:
     
    $myname 
    'EXT';
     
    function 
    myName(){
    global 
    $myName;
    unset(
    $myName);
    }
    myName();
    echo 
    $myName;
    It will still echo EXT 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:
    //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($EXT == '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:
    $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:
     
    <?php
     
    $Welcome_Start 
    'Welcome';
    $Username 'EXT';
    $Welcome_End 'to your profile';
     
    /*
    This is the [B]WRONG[/B] way to join them strings together.
    */
     
    $Welcome "$Welcome_Start $Username $Welcome_End";
    //OUTPUT: Welcome EXT to your profile
     
    /*
    This is the [B]CORRECT[/B] way.
    */
     
    $Welcome =  $Welcome_Start.' '.$Username.' '.$Welcome_End;
    //OUTPUT: Welcome EXT 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.

    Here's the 4 variations of these types of mathematical operators

    * Increment
    o $number++
    o ++$number
    * Decrement
    o $number--
    o --$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 number 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:
    <?php
    $number 
    5;
     
    //POST
    echo 'I should be 5 : '.$number'<br />';
    echo 
    'I should be 5 : '.$number++. '<br />'// 
    here I've 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
    Okay, 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/function.error-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:
        //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

    There's not really much to put in here apart from I hope you enjoy learning some PHP and comment like mad if you like.

    Thanks,
    EXT.
     
< Can somone tell me the names for post count | Can somone tell me the names for post count? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site