Try Tuts+ Premium, Get Cash Back!
Winners Announced: Free Copies of “Pro PHP and jQuery”

Winners Announced: Free Copies of “Pro PHP and jQuery”

Jason Lengstorf was nice enough to offer our community a handful of copies of his latest awesome book, Pro PHP and jQuery. Be sure to read his latest Nettuts+ tutorial, “Object-Oriented PHP for Beginners,” based on his book.


Winners Announced:

Congratulations to the following winners, who were randomly selected:

  • Jonathan Stuckey
  • Rick Blalock
  • Matt Vickers

Each of you will be contacted shortly about arranging your free copy. Thanks again to everyone who entered!

“This book is for intermediate programmers interested in building Ajax web applications using jQuery and PHP. Along with teaching some advanced PHP techniques, it will teach you how to take your dynamic applications to the next level by adding a JavaScript layer with jQuery.”

To enter for a chance to win a hardcopy version of the book, leave a comment with a PHP tip. It can be as simple or as long as you like. Just as long as it was something that helped you to learn PHP, that will automatically enter you! On Monday morning, we’ll choose the winners!


You can also purchase “Pro PHP and jQuery” here.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • David

    I seem to be using this alot to check to see what my sql query looked like, or what a variable is set to at runtime:

    $handle = fopen(“log.txt”,”a+”);
    fwrite($handle,$error.”\n\r”);
    fclose($handle);

    replacing $error with whatever message/variable you want to use.

  • MathieuL

    Use Sanitize Filters!

  • http://thedigitalyardsale.com Digital Yard Sale

    My best PHP tip? Let me get my hands on a copy of that book and then I’ll be full of them. As for now, I’m sort of a novice.

    My advice? Steal code and play around with it. Figure out what works and what doesn’t. Get comfortable with the language and make yourself at home, making mistakes along the way.

  • nestor

    Thanks for the great tutorials nettus! I hope I can win the book; I’m in need of few good books

    Thanks for the opportunity

    Nestor Rojas

  • Gaurav Sharma

    You can only truly start reaping the benefits of PHP once you move away from procedural programming and start using classes and objects.

  • http://laranz.com lawrence77
    • http://laranz.com lawrence77

      txt =”Hello nettus, i want to win :) “;
      echo $txt;

  • Wilfred Khalik

    The fastest way to learn Php is to join Lynda.com and watch the easy to follow vids…worked like a charm for me

  • http://www.flickr.com/photos/michbukana Michbukana

    echo(‘Good luck boys ‘);

  • http://www.ichilton.co.uk Ian Chilton

    Always use mysql_real_escape_string for all variables contained in SQL statements and htmlentities() when displaying variables!

  • Simon Hudson

    Don’t just fix your errors – make every effort to fix your warnings as well. Even though things still (generally) work with warnings, treating them as things which need to be fixed before release is a tremendously valuable.

  • Fallenclient

    Bookmark php.net/docs.php for reference, test often, build slowly and start out by looking at frameworks/source code and open source php apps to get a feel for it. Make small changes, experiment and enjoy what your doing. Once your happy then take the plunge in to bigger and more complex php coding. Again, test often and build slowly until you are confident.

  • sk077

    Use tags when using print_r() function for better lisibility.
    For instance :

    Hope i can win this book !

  • sk077

    Use tags when using print_r() function for better lisibility.
    For instance :

    Hope i can win this book !

  • Dieter Struik

    Don’t use custom PHP template systems for HTML pages. PHP is a templating system by itself.

    Somehow you’ll end up using conditions and loop constructions… asking yourself in the end…. with pure PHP this was simple to code.

  • http://www.twosocks.com.au Carl Crowther

    lynda.com is the best advice I can give you! $25 a month is nothing do it and you’ll never regret it!

  • klittle

    For speed I generally use CI + jquery it so simple to get things working…

  • http://www.pregoafundo.com joaquim sampaio

    One more to the run! lol

  • KheirEddine
    • KheirEddine

      $text =”Hello nettus “;
      echo $text;

  • http://twitter.com/nikolamaravic Nikola Maravic

    Visit –> php.net <– and find some useful functions you probably didn't know about

  • safipeti

    In a for loop do not call the size/count function at every loop:

    $till = count($array);

    for($i = 0; $i <= $till; $i++){

    //do something here;
    }

    • http://www.dradept.com David

      I would add a few lines to this to ensure it is worth even starting a loop.

      $till = count($array);

      if ($till > 0)
      {
      for($i = 0; $i <= $till; $i++){
      //do something here;
      }
      }

      • Gabriel

        David, The condition will fail before it starts the loop, no need to check beforehand.
        also since count($array) can’t be negative, you could just say if($till)

        I like the for statement, however for very simple loops like to use,
        $till = count($array);
        while($till){
        $till–; // decrement the $till value usefull as $array[$till] will get you the corresponding element
        //do something
        }
        once $till hits 0 it returns false and will exit the loop.

  • http://www.novarossi.it Peter

    lol @ KheirEddine

  • http://www.romaindesplanques.com Romain

    Instead of using this :
    if (strlen($string) > 10) {}
    you can use :
    if (isset($string[10])) {}

    It’s faster

  • Marcus

    echo ”;
    print_r($array);
    echo ”;

    outputs an array in very readable format…

  • http://shutterskills.com Rish

    Php was the past, is the present and will be the future :)

  • http://www.noakhalionline.com Nur Mohammad

    Its so Funny…………….. Ha Ha Ha HA AHaaaaaaaaaaaaaaaaa

  • Ilia Malchenko

    If you have a lot of elseifs within an ifstatement, consider using a switch statement. Once you get used to it, it’s a lot easier and quicker to write, and I believe that the code gets executed quicker, as well.

    So, for instance, we have a variable $num that holds an integer value from 0 to 10, and depending on what that number is, we want it to do different things. Our if-statement would look something like this:

    if($num == 0) {
    // Do something
    }
    elseif($num == 1) {
    // Do something else for 1
    }


    elseif($num == 10) {
    // Do something else for 10
    }
    else {
    // the number wasn’t from 0 – 10
    }

    Now, our switch statement for the same case would be as follows:

    switch($num) {
    case 0: // This is same as if($num == 0)
    // Do something
    break;

    case 1: // This is same as elseif($num == 1)
    // Do something else for 1
    break;


    case 10: // This is same as elseif($num == 10)
    // Do something else for 10
    break;

    default: // This is the same as our else{}
    // the number wasn’t from 0 – 10
    break;
    }

    Hope this helps!

    Let me know if you have any questions.

    • Ilia Malchenko

      Oh, and forgot to add. If you want to see further explanation and more examples, visit PHP.net switch manual.

  • icomir

    Always put session_start() on top.

  • TAKA

    First.
    It’s more faster foreach than for.

    Second.
    If php method return itself,
    you can write below.

    out(‘I’)->out(‘want’)->out(‘book’)->out(‘please.’)->out(‘:p’);

  • http://None Takaa

    Echo 123

  • Mauro Fernandez

    Comment everything, you don’t know when you will get back to that file and try to fix something.

  • http://edgarquintero.me edario

    Tip: Read all these tips

  • http://www.austinpickett.com Austin Pickett

    Probably a bit too late to enter but it’d be really sweet if I won!

  • Tim Smith

    Explode is an amazing thing to use. Ive used to generate Clean urls with sites I develop

    $entry = explode(“/”, $_SERVER['REQUEST_URI']);
    $page = $entry[1];
    $var1 = $entry[2];
    $var2 = $entry[3];
    $var3 = $entry[4];

    This could be used to generate something … http://www.yoursite.com/page/addcomment

    This is also just an example :)

  • justpassinthru

    Three of the most useful php lines I’ve ever learned:

    1. print ” . get_defined_vars() . ”;
    2. print ” . get_defined_functions() . ”;
    3. print ” . get_defined_constants() . ”;

    Any of which you can array_keys to as well (ie print ” . array_keys(get_defined_vars()) . ”;)

  • Mateus

    echo “t3st”;

  • Ricardo Cino

    Always use single quotes, because it’s faster and because you can’t put variables in single quotes because php just won’t read it, you have to put the variables out of the quotes what is more easy to read for other developers. :)

    • http://www.jeffrey-way.com Jeffrey Way

      That’s not true.

  • conor

    $_GET is for non-sensitive data and searches on existing data, $_POST is for sensitive data and longer bodies of text

    im a newbie

  • Wilfred Khalik

    I am surprised that there is no mention of Lynda.com until the the 14th page….research has shown that the fastest way to learn is through the use of as many senses as one has….hearing + seeing..etc….and by practicing….the first part is taken care of terrifically by Lynda.com-you have a over 30% retention of information as compared to reading a book or by attending a lecture…because you can always go back to a topic that you found difficulty in-I think it is priceless….because your time is!!

  • Wilfred Khalik

    Well…the second part of what I am talking about above can be taken care of by all the other wonderful advise that has already been given…

  • A.

    var_dump() everything! (wrap it in if you don´t use a console).

  • syetuya

    function t($str) {
    if(is_array($str)) {
    return array_map(‘t’, $str);
    }
    return trim($str);
    }

  • André

    When learning PHP, pick a pet project to work on, it’ll make the concepts really stick.

  • http://blog.livedoor.jp/laddy/ Laddy

    // over the proxy. get xml file
    $jsonUrl = ‘http://jsonurl/api’;
    $proxyData = array(
    ‘http’=>array(
    ‘proxy’=>’tcp://[proxy url]:[port]‘,
    ‘request_fulluri’ => true
    )
    );

    $json = file_get_contents($jsonUrl, false, stream_context_create($proxyData));
    $mei = json_decode($json,true);
    var_dump($mei);

    echo $mei;

  • http://desired.jp/ Ryuichi Nonaka

    //detox function
    function detox($d, $lang = “Japanese”, $encode = “UTF-8″)
    {
    //エンコーディング処理
    mb_language($lang);
    $d = mb_convert_encoding($d,$encode, “auto”);

    //無害可処理
    $d = htmlspecialchars($d,ENT-QUOTES);
    $d = addslashes($d);

    //前後の空白削除
    $d = trim($d);
    return $d;
    }

    • abhijit

      $d = htmlspecialchars($d,ENT-QUOTES);

      It should be ENT_QUOTES – just FYI

  • skyliner

    Tip1 – If you’re just starting out and PHP looks weird, just keep at it. The more you’ve looked at it the more normal all that square brackety stuff is.

    Tip 2 – If you’re having trouble remembering some code, get out the old-fashioned paper and pen, and write it out several times.

    Tip 3 – Buy a good book on PHP security and read it.The security books are scary, but don’t be intimidated into never building another app. Remember, there is always someone somewhere who will be able to hack your site whatever you do, but you have to make it as inconvenient for them as you can, so that hopefully they will give up and try with someone else’s app. Never, ever, ever trust user input! Always validate and sanitise before displaying or storing in a database.

    Tip 4 – PHP is awash with functions for dealing with strings and arrays. Check the manual at php.net first before rolling your own!

    lots of good tips in this thread, really enjoyed reading them, thanks guys!

  • Darren L.

    Here is a tip:

    Use ternary when you can, it will save you from a lot of coding:
    turn this:
    if ($test == ‘norwegian’) {
    echo ‘test is norwegian’;
    } else {
    echo ‘test is generic’;
    }

    into this

    echo ($test == ‘norwegian’) ? ‘test is norwegian’ : ‘test is generic’;

    It helps out a lot!

  • http://www.dradept.com David

    Get a variable from everywhere

    If not already set then check POSTed then check GET otherwise set to default value

    if (!isset($var)) { if (isset($_POST['var'])) { $var = $_POST['var']; } elseif (isset($_GET['var'])) { $var = $_GET['var']; } else { $var = “NO”; } }

    Clean it up and remove dodgy bits

    $var = trim(htmlspecialchars(stripslashes($var)));

  • http://www.e-x-e.dk Thomas Stig Jacobsen

    Learn to use ternary operators for e.g. setting variables. This feature will save you lines of code and the notation is quite simple when you first have gotten the hang of it.

    $mode = ( isset($_GET['bar']) && ! empty($_GET['bar']) ) ? $_GET['bar'] : ‘default’;

    And the most important tip: Don’t just follow random tutorials to do a lot of code! Truly understand the code you’re writing and by that you can write the most secure and stable code.

  • Abhijit

    Looks like I got too late :( …!!

    Still adding a tip :) ….

    You can use array_map() function to apply a function to each element of an array. This works for php builtin functions too (unlike in case of the array_walk() function). Below is an example –

    $list_str = ‘John, Doole,Toole, Mark, Bob’;
    $list = explode(‘,’, $list_str);
    print ” . print_r($list, 1) . ”;
    $list = array_map(‘trim’, $list);
    print ” . print_r($list, 1) . ”;

    Try it and Congrats to those who won!!!

    • Abhijit

      The first line should have been like this $list_str = ‘John,  Doole,Toole, Mark,   Bob’;

  • Rochester

    One tip for code comments..

    At the end of each block, use comments this way:

    // [block] end */

    so, when you want to comment this entire block, go to the begining and put the start of the comment, this way;

    /* [block] start, comment start

    code, code, code

    // block end */

    So you will not comment your entire code, or need to search for the block ending.

    []‘s
    Rochester