PHP Tips and Tricks

php

After years use PHP, there is always a time where I find myself look back to PHP documentation.

PHP has many functions, if you’re new to PHP, you won’t note some of useful function to remember.

There are also some of technique that you couldn’t learn by just looking PHP documentation.

For a beginner, who just learning basic PHP programming, I have collect some of tips and trick to help you upgrade your skill.

  1. Generate random password

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    <?php
     
    function generatePassword($length) {
        $character = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $password = "";
        for($i=0;$i<$length;$i++) {
            $password .= $character[rand(0, 61)];
        }
        return $password;
    }
     
    echo generatePassword(7);
     
    ?>
  2. Calculate age

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    <?php
     
    $testcase = array("12-03-1985", "15-01-1987", "01-01-2000");
     
    function getAge($year) {
        return floor(abs(strtotime('now') - strtotime($year))/31536000);
    } 
     
    foreach($testcase as $test) {
        echo "Born in $test, approximately age is " . getAge($test)." years<br/>";
    }    
    ?>
  3. Get visitor’s IP address

    1
    2
    3
    4
    5
    
    <?php  
     
    echo "Your IP address is " . $_SERVER['REMOTE_ADDR'];  
     
    ?>
  4. Check even or add number

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    <?php
     
    $testcase = array(1, 4, 5, 6, 11);
     
    function isOdd($number) {
        if ($number % 2 == 0) {
            return false; 
        } 
        return true;
    }    
     
    foreach($testcase as $test) {
        if (isOdd($test)) {
            echo $test . " is odd<br/>";
        } else {
            echo $test . " is even<br/>";
        }   
    }    
     
    ?>
  5. Get file extension

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    <?php
     
    $testcase = array("sample.txt", "sample.jpg", "sample.case.txt");
     
    function extension($filename){
        return substr(strrchr($filename, '.'), 1);
    }
     
    foreach($testcase as $test) {
        echo "Extension from $test is " . extension($test) . "<br/>"; 
    } 
     
    ?>
  6. Sort data in array

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    <?php
     
    $data_list = array("lychee", "pineapple", "apple", "mango", "strawberry", "banana", "orange", "grape", "guava");
     
    sort($data_list);
    foreach($data_list as $data) {
        echo $data . "<br/>"; 
    } 
     
    ?>
  7. Random pick from array

    1
    2
    3
    4
    5
    6
    7
    
    <?php
     
    $fruits = array("lychee", "pineapple", "apple", "mango", "strawberry", "banana", "orange", "grape", "guava");
     
    print $fruits[array_rand($fruits)];
     
    ?>
  8. Shuffle data

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    <?php
     
    $data_list = array("lychee", "pineapple", "apple", "mango", "strawberry", "banana", "orange", "grape", "guava");
     
    shuffle($data_list);
    foreach($data_list as $data) {
        echo $data . "<br/>"; 
    } 
     
    ?>
  9. Highlight text

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    <?php
     
    $text = "Sample sentence from KomunitasWeb, regex has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor";
     
    $text = preg_replace("/\b(regex)\b/i", '<span style="background:#5fc9f6">\1</span>', $text);
     
    echo $text;
     
    ?>
  10. Change to title case (first letter uppercase)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    <?php
    $testcase = array("hello world", "Hello WORLD", "heLLO world");
     
    function titleCase($text) {
        return ucwords(strtolower($text));
    }
     
    foreach($testcase as $test) {
        echo titleCase($test)."<br/>";
    }    
     
    ?>
  11. Time since

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    
    <?php
     
    $testcase = array("12-03-1985", "15-01-2007", "04-04-2009", "01-01-2009", "30-03-2009", "07-04-2009", "07-04-2009 11:12");
     
    define("MINUTE", 60);
    define("HOUR", 3600); // 60 * 60 
    define("DAY", 86400); //  60 * 60 * 24 
    define("WEEK", 604800); // 60 * 60 * 24 * 7
    define("MONTH", 2592000); // 60 * 60 * 24 * 30
    define("YEAR", 31536000); // 60 * 60 * 24 * 365
     
    function timeSince($date) {
     
        $since = abs(strtotime('now') - strtotime($date));
     
        if($since > YEAR) {
            $year = floor($since / YEAR);
            return "more than $year year(s) ago";
        }    
     
        if ($since > MONTH) {
            $month = floor($since / MONTH);
            return "about $month month(s) ago";
        } 
     
        if ($since > WEEK) {
            $week = floor($since / WEEK);
            $day = floor(($since - ($week * WEEK)) / DAY);
            return "about $week week(s), and $day day(s) ago";
        }
     
        if ($since > DAY) {        
            $day = floor($since / DAY);
            $hour = floor(($since - ($day * DAY)) / HOUR);
            return "about $day day(s), $hour hour(s) ago";
        }
     
        if ($since > HOUR) {        
            $hour = floor($since / HOUR);
            $minute = floor(($since - ($hour * HOUR)) / MINUTE);
            return "about $hour hour(s), $minute minute(s) ago";
        }
     
        if ($since > MINUTE) {        
            $minute = floor($since / MINUTE);
            return "$minute minute(s) ago";
        }
     
        return "under 1 minute ago";	
     
    }
     
    foreach($testcase as $test) {
        echo "Time since $test is " . timeSince($test)."<br/>";
    }   
     
    ?>

Don’t miss a thing, subscribe to our feeds. Or follow us on Twitter.

Related posts:

  1. 101 PHP Tutorials for PHP Programmer Wannabe
  2. Codeigniter tutorial – To do list application
  3. Using Django template in PHP
  4. 10 Practical PHP Regular Expression Recipes
  5. CodeIgniter Tutorial – To do list application#2

Tags:

6 Responses to “PHP Tips and Tricks”

  1. [...] original here: PHP Tips and Tricks | KomunitasWeb Tags:return-false, return-true Category:PHP Array [...]

    Pingback by PHP Tips and Tricks | KomunitasWeb - 【23php】 — April 7, 2009 @ 2:44 am

  2. I love your articles. Thanks. Just simple tips. I always have to look little things like this up. And I still thank you every day for referring me to h2o

    Comment by Chuck — April 7, 2009 @ 6:12 am

  3. @Chuck, You’re welcome. I really like Django templating, that’s why I love H2O too.

    Comment by Gilang Chandrasa — April 7, 2009 @ 7:18 am

  4. Awesomeness. >:)

    Comment by Stalyn — April 7, 2009 @ 5:07 pm

  5. [...] PHP Tips & Tricks (komunitas) – EN [...]

    Pingback by PHP Linkliste « H79.de — April 10, 2009 @ 8:12 am

  6. Hi,

    FYI: you have re-posted the “get file extension” code as “7. Random pick from array”.

    Thanks for the excellent tips (esp. CodeIgniter).

    Comment by Andy Wright — April 27, 2009 @ 5:50 am

Additional comments powered by BackType