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
  • http://jajarvin.freeunix.net jajarvin

    Hello

    Global variables in human-readable form:

    Bye

  • http://jajarvin.freeunix.net jajarvin

    Hello

    Global variables in human-readable form:

    print_r ($GLOBAALS);

    Bye

  • Wayne

    More about output then you could imagine :)

    Print(“MSG”); will output “MSG” to the browser or console if you are running PHP as a binary.You can include $vars but not Arrays. if you try to echo an array you will see something like “Resource ID #7″

    echo “MSG”; will output “”MSG” to the browser or console if you are running PHP as a binary. You can include $vars but not Arrays. if you try to echo an array you will see something like “Resource ID #7″. echo() allows for multiple strings to be printed at once where as print() can only do one string at a time.

    print_r(array) will give you the detail of an resource

    $array = array(“name” => “joe”, “id” => “15″);

    echo $array; = “Resource ID #3″;

    print_r($array); = Array { “name” => “joe”, “id” => “15″ }

    print_r() will not work on a page that has been loaded via Ajax (jquery, etc) so you have to send the output to the error_log(); One catch is that error_log only takes a string and “print_r($array)” is an array resource so… try this:

    $arrayAsString = print_r($array, true); then you can call error_log($arrayAsString) and will see output in the error log to track whats going on.

    when you add the TRUE flag, print_r returns a string rather than a resource.

  • Drewski

    Never forget to open and close your php code segments using the tags!

  • http://www.nettuts.com/ Dave

    The one trick with PHP I have learned was from another programmer’s inconsistency. You must be consistent with http://www.domain.com as oppose to domain.com. Because sessions will treat these two websites as different directories!

  • http://www.aricbeagley.me Aric

    Don’t be afraid of ternary operators. Code can be simplified / minified using simple elegant ternary operators for changing state like so:

    $name = “John”;
    if($name != null) {
    echo $name;
    } else {
    echo “Guest”;
    }

    Can be simplified as:

    $name = “John”;
    echo $name != null ? $name : “guest”;

    As such the simple syntax for the ternary IF statement is like so:

    *command – echo / variable definition* *condition* ? *do this if true* : *do this if false*;

    I was seriously confused the first time I saw these but after a little playing around you can see how great they really are.

  • iivo

    Basic and simple tip, but actually handy. When writing a copyright notice for website (as seen on footers), use (taking this page as an example):

    Copyright © Envato

    • iivo

      The code was stripped down or something. Anyway, so use it like this (and drop the extra whitespace):

      Copyright & c o p y ; Envato

  • http://lookitsatravis.com Travis Vignon

    I think my favorite trick lately has been using a session var to make sure people access my app in the right order. For example, I recently built an ecommerce site, and one of the challenges in this particular set up was that the customer has to access the pages in a specific order to make sure they get the correct products (highly specialized products). On each page of the app EXCEPT the index page, I added this:

    If a cart object has never been created (meaning they didn’t go to the index.php page first), we can assume the user bookmarked the wrong page, or simply followed a bad link or some other way of getting to the app incorrectly and it will redirect them back to the index file and they can follow the process in the right order.

    I use header() pretty liberally to control app flow. Learn it!

    • http://lookitsatravis.com Travis Vignon

      Well, assuming once my comment is accepted my code will not display, here’s what I was trying to type, although i’m leaving out all quotes in hopes it will display:

      include_once(cart.css);

      session_start();
      $cart =& $_SESSION[new_cart]; if(!is_object($cart)) header(Location: http://www.domain.com/shop);

      If that still doesn’t show up…well….then I guess I don’t deserve a copy! ^-^

  • http://www.cbesslabs.com Bratu Sebastian

    I want to win

  • http://www.websiteperks.com Nelson

    Variables must start with a dollar sign $var

  • http://www.webscape.lv Ervīns Jekovičs

    Tip $i++:

    Before you start programming in php you should understand how http protocol works..

  • Bhargav Patel

    Remember that Variables are case sensitive. Its a good practice to make first letter small and every new word’s first letter capital like so…

    yourNameField

    instead of your_name_field or yournamefield

    yourNameField is faster to type and easier to read.

    Remember that Variables are case sensitive!!!!

  • http://fusion2004.com fusion2004

    In PHP, you don’t have to declare variables to be a certain primitive type or class.

    By setting $var = 1, $var is now an integer. If I set $var = “test”, it’s now a string!

    In order to check the type of a variable, use gettype($var). When testing for a certain type (i.e. in if statements), you can use other nifty functions like is_bool($var), is_string($var), is_numeric($var), etc.

    This is just one of the many things that make coding in PHP much easier than most other programming or scripting languages for me.

  • pasco

    Always remember to say hello before you start programming otherwise php will get mad and your program wont run until you do it.

  • http://liamhammett.me/ Liam Hammett

    $var = $value; // This is good.
    $var = “$value”; // This works, however quotes are not necessary and just waste characters…
    $var = ‘$value’; // This does not parse correctly, so it will not work.

  • Simon Jensen

    its a good idea to echo out with

    echo “”;
    print_r($_POST); //Or whatever you want to see if it echo out
    echo “”;

    the will set it nice out so its nice to read :)

  • http://www.jtdportfolio.com Jason

    Ternary operators save my code a lot of extra lines.

    // Example usage for: Ternary Operator
    $action = (empty($_POST['action'])) ? ‘default’ : $_POST['action'];

    // The above is identical to this if/else statement
    if (empty($_POST['action'])) {
    $action = ‘default’;
    } else {
    $action = $_POST['action'];
    }

  • http://www.ebrentnelson.com E Brent Nelson

    All the good tips were taken. :( Dang me and my slowpokesqueness.

  • http://www.andrews-custom-web-design.com Andrew Perkins

    I don’t know if this has been said but I really like the ternary operator for quick one liner if/else statements:

    $greaterVal = 10;
    $lesserVal = 5;

    Ternary Operator:
    $answer = ($greaterVal > $lesserVal) ? ‘true’ : ‘false’;
    echo $answer; // true

    Using If/else instead:
    if ($greaterVal > $lesserVal) {
    $answer = ‘true’;
    } else {
    $answer = ‘false’;
    }

    echo $answer; // true

  • AP

    I cannot tell you how many times I’ve been stuck with an atrocious hosting provider who doesn’t even give me simple yet necessary server information. This little kicker saves my butt quite a lot… Now if only it could recommend a good host, haha.

    • AP

      <?php

      phpinfo();

      ?>

  • http://www.bitrepository.com/ Gabriel

    Learn how to manipulate strings:


    function extract_unit($string, $start, $end)
    {
    $pos = stripos($string, $start);

    $str = substr($string, $pos);

    $str_two = substr($str, strlen($start));

    $second_pos = stripos($str_two, $end);

    $str_three = substr($str_two, 0, $second_pos);

    $unit = trim($str_three); // remove whitespaces

    return $unit;
    }

  • http://nuancetone.com mike varela

    echo ‘I love to read PHP books’;

  • http://3ansdemulti.com Patrice Poliquin
  • Eugene

    It will save you some typing

    echo str_reapeat(”, 1000);

    :)

  • eranZ

    $var = $value; // ok
    $var = “$value”; // ok, but double quotes are not necessary
    $var = ‘$value’; // will not work (single quotes will not allow parsing)

  • edanTal

    Tip #1: Use full paths in includes and requires, less time spent on resolving the OS paths
    Tip #2: ++$i is faster than $ i++, so use pre-increment where possible.

  • http://www.shekhardesigner.com $hekh@r

    <?php
    echo 'Hello world! ‘;
    echo ‘I am in’;
    ?>

  • http://visualmagick.com Dan

    I found netuts…..all I needed

  • http://photoshop.dfreebies.com Asker

    Use a Framework -The most valuable PHP tip ever! ;)

  • Lukas Orgovan

    What I am using almost in every project is: if(!isset($_POST["value"])) { header(“Location: path”); }

    Also, there was an article on Smashing Mag., where they use something like this:

    if (isset($password[7])) { // do something }

    It checks, if the variable password is at least 8 characters long (the first char in array is [0], so 7 means 8). They refer, that it is a bit faster then using strlen() function.

  • http://www.aldrinponce.com/ aldrin

    As a php coder it would be good to practice to declare variable with values.

    You can use

    error_reporting ( E_ALL );

    at every beginning of your php file and you will see bunch of errors. Even a simple undeclared variable.

  • http://qmmr.pl Marcin

    If you ever find yourself in a doubt, php.net is the way to go, best advice ever!

  • kosaidpo

    echo ‘i hop i can win : D’

  • http://www.pickledshark.com Carey

    Here are a couple of php tips I like..

    To add a bit more text to the end of a variable, you would normally do:

    $text = ‘Frog’;
    $text = $text.’ Soup’;

    Instead you can do:
    $text .= ‘ Soup’;

    An easier way to echo php inside html:

    And a shorthand way to do an if else statement:

    $frog = ($colour == ‘green’ ? true : false);

    You can also use this in combination with the last tip:

    Is it a frog?

  • Abhishek
  • http://www.wlab.be Arno

    There are a lot ways to learn a coding language, internet, books, courses, etc. Each has it’s own advantages, but for me there’s nothing better than a well written book. Why, no distraction – good examples – easy to understand – always available and much more reasons. I prefer to learn with books, so please help me learn more from PHP.

  • http://jubstuff.netsons.org Just

    Install and use FirePHP to debug your php code.
    I’ve begin recently and I’ll never turn back :)

  • Loken

    jQuery is great but with PHP it’s awesome…

  • http://fotos.nitosblog.com nitos

    If you have any difficulties, you can always ask on twitter (faster response), check on websites like W3schools, etc :)

  • http://hmd-webdesigns.com/ Hugo

    When using HTML in an echo statement always use ‘ instead of “.

    For Example:

    This works;

    echo “This is a test!”;

    This doesn’t;

    echo “This is a test!”;

    ” closes off the echo, whereas ‘ doesn’t.

  • Michael Hanley

    Watch out for operator precedence when beginning PHP, I would recommend using just the “&&” and “||” operators for beginners or you might get some unexpected results.

    Examples:

    The first will return to TRUE because it’s read as (false AND false) OR true, while the second will return FALSE because it’s read as false AND (false OR true).

    • Michael Hanley

      The PHP tags where stripped, could a mod please remove this post.

  • Michael Hanley

    Watch out for operator precedence when beginning PHP, I would recommend using just the “&&” and “||” operators for beginners or you might get some unexpected results.

    Examples:
    var_dump(false && false || true); //TRUE
    var_dump(false AND false || true); //FALSE

    The first will return to TRUE because it’s read as (false AND false) OR true, while the second will return FALSE because it’s read as false AND (false OR true).

  • ethan Kramer

    to start a php document you need to put the ” tag at the end of the document (without the quotes) and put your php content in between those two tags.

  • Ethan Kramer

    Sorry about the previous comment it did not come out as i planned so i posted again hoping it would appear correctly this time.

    to start a php document you need to put the “<?php" tag at the begining of the document (without the quotes) and put the "<?" at the end of your document and put your php content in between those two tags.

  • http://hector-lee.com Hector Lee

    I was amazed how long it took me to sort out my issue with single and double quotation marks.

    Single quotation marks are treated literally.

    Double quotation marks are interpreted.

    • http://datasplash.co.uk Darren Lunn

      What a great, concise description, Hector. I’m just learning this stuff, and advice like that is priceless..

  • http://www.kisanbhat.com Kisan

    Short If-Else Structure

    $var = [condition] ? [true] : [false];

    e.g.

    $freebook=”Pro PHP and jQuery”;

    echo (‘jQuery’ == $freebook ) ? ‘jQuery’ : ‘Pro PHP and jQuery’;

    // will echo ‘Pro PHP and jQuery’

  • joren

    Use the short if else structure, in simple situations e.g:
    $background = $dark : ‘black’ ? ‘white’;

    this is the same as:
    if($dark == true) {
    $background = ‘black’;
    }
    else {
    $background = ‘white’;
    }

    • skyliner

      mate, you’ve got the ternary operator’s syntax the wrong way round, it should be
      condition ? true : false;

      • joren

        Thanks, i had it wrong

  • http://mileonemedia.com Joe Cianflone

    I’m going to offer 1.5 tips:

    The .5: Frameworks are great and you should use them, but, please learn the language FIRST. Any language, spend a couple of months, maybe even do a small project without using a framework. Once you get the that under your belt you can take the time to learn a framework. My choice: CodeIgniter

    OK, now a PHP tip:

    == vs ===

    $age1 = “20″;
    $age2 = 20;

    $age1 == $age2 //is true
    $age1 === $age2 //is false because the variables are of different types

    so use === when you need to make sure your types are the same as well as the values set

  • http://www.twitter.com/ooredroxoo Rafael Nascimento Sampaio

    PHPs blockcodes within html

    do something

    do nothing

    and

    no { or } in if, for and foreach conditions
    so
    if(condition): elseif(condition): else: endif;
    is the same that if(condition){}elseif(condition){}else{}
    and for(interator; condition; increment): endfor;
    is the same that for(interator; condition; increment){}
    and foreach(array as $key=>$value): enforeach;
    is the same that foreach(array as $key=>$value){}
    so now no more curly brackets hell for me.

    • http://www.in-the-attic.co.uk Garry Welding

      You could also achieve the same with a Ternary Operator:

      (condition?true:false); //standard if else operator in ternary format.

      (condition?true:(condition?true:false)); // if elseif else modification to the ternary operator.

      The brackets are used to avoid confusion when stacking ternary statements.

  • Diogo Doreto

    Learn Regular Expressions! They’re a powerful way for parsing strings. Search for patterns and, if you need to, replace then. You can use from simplest to more complex strings patterns, use it for validate e-mails, urls, for remove html tags from texts and many other things. It’s worth the time spent for it.