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.

Using classes instead of loss functions.
Debug, debug, debug!!!
Seriously, the hardest part for me in learning PHP was getting good at debugging the language, since the errors messages are often times very cryptic.
Don’t reinvent the wheel and remember that the fastest code is the code that never runs.
Use to out what is installed on your server and what version it is.
If the whole file is PHP
you don’t have to finish the file with “?>”
Even if the file is not PHP only, you always can skip the finishing ?>
Unless you have HTML after your PHP block ;) Then you might run into problems not closing it off.
Always keep your codes clean and clear,
Its easier to update or maintain your code.
Try and make your classes as independent of the current project as possible. This way you’ll be able to reuse your code and save LOADS of time.
Hi,
$variable = $variable + 1; is the same like $variable ++;
If your going to start writing PHP, know a good amount of coding first.
if YOU’RE….
PHP is a easy language (you don’t need to declare variables, for example).
My tip is to always use the maximum level of error_handling and stick to it. Don’t let any WARNING or NOTICE slip. If you program like this, you learn faster and eventually make better and quicker code.
Agreed. I didn’t learn this way and wish I had.
Comment Comment Comment…… comment your code and when you come back to a project three weeks later you won’t have to dig in for 2 hours before you know what you did!
Saves time and helps keep things straight.
Use instead of to increase the compatibility of your PHP scripts in different servers.
The filter of NETTUTS don’t left I use the PHP tags. Then, other tip:
” Separe your site in some PHP files, like menu.php, footer.php, content.php. Any alterations will be easy to be executed if you use this model. Don’t forget of create a PHP file that have a function that do the connection with the DataBase, and use this file in all the site, if you need change the DataBase login, for example, You only need change this file. “
One of the best tips I received about PHP that’s helped me debug several scripts is to make a small function in one of my helper files called darray.
function darray($array, $exit = true)
{
echo ”;
print_r($array);
echo ”;
if ($exit)
{
exit();
}
}
That way whenever I’m using arrays, I can easily see what values are being passed in by just typing
darray($arrayName);
I use something like
echo “”;
print_r($array);
echo “”;
My pre tags were stripped, probably yours too
One more time.
echo “ pre ”;
print_r($array);
echo “ /pre ”;
What’s wrong with using var_dump($array); instead?
A similar function I use is:
function pprint_r($p) {
print “”; print_r($p); print”";
}
Obviously can be modified to pass the exit param too. Gives a nice clean view of the array/object.
Micro-Performance tip: Use single-quotes instead of double-quotes for strings if you don’t have variables-to-be-expanded in the string.
$slower =”ohoh”;
$better = ‘yeah’;
Next level, PHP and jQuery can build huge application, this books will be helpfull.
Don’t use spaces to format your code, use tabs. It will let your code run a little bit faster.
Structuring code to line up is a huge time saver :O
Always remember to sanitize all form inputs, removing unwanted characters and html tags to help preventing injections (I know it is simple but I just started with php, I’m a AS3 developer).
Thanks!
When I have learned MVC, everything was better. I haven’t now a mess in code.
$array[’id’] around 10 times faster than $array[id]
Once you have learned the PHP basics and have some understanding of PHP functions and classes, try to do some projects with CakePHP, CodeIgniter or Zend Framework. Look at the code of the different frameworks and try to understand it. This has helped me a lot to become a better PHP programmer.
Best way to debug: Print statement.
Also! I love conditional assignments: $foo= ($bar == 0) ? $foo++ : $foo–;
Using switch statements to match simple variables instead of if/if else.
‘$variable’ is not your friend
If you get this warning message, while running a php document:
‘Warning: Cannot modify header information – headers already sent by (output started at filename line no __) ‘
This means that an error has occured in your page. This happens if there are any executable statements (Like echo) before a session variable or cookie. Find this statement, remove, and then refresh the page.
Also, one space at the top of your page (line 1) will cause an error in your php. Remove this space, and refresh the page.
Not so much a php tip (although useful for php) is learn a framework. It takes additional time up front, but your productivity will increase dramatically in the long run as you won’t end up “reinventing the wheel”.
Easier control structure :
if ($var== ‘test’) {
$i++;
}
Can be replaced by :
if ($var== ‘test’) $i++;
Added much clarity to my work
Use MVC and make it simple :)
Get to know to documentation, read other peoples code and do research, if you have a problem and cannot find an answer, ask but always post as much as possible if you expect help.
Oh and ?: helped me out so much, I like If {…} else {…} but for something short and simple ?: if the way to go!!
That’s my usual footer :-)
Copyright © 2000- website.com
Of course edit 2000 according to the right starting year.
I assume you typed:
Copyright © 2000- except with the correct PHP tags and quotations around Y, right?
God Bless.
You’d think since they asked for PHP snippets that they’d at least allow them in comment blocks, right? This is killing me.
Anyway – Luca DB’s tip is to use echo date(‘Y’); in your footer to always keep your copyright year current.
If that doesn’t show up, I quit.
Try to use single quotes (‘) instead of double quotes (“) because when using double quotes PHP tries to interpret variables.
Wrong:
$var = ‘The cat is $color’;
Good:
$var = ‘The cat is ‘ . $color;
Easily pull a URL to pieces using the parse_url() function:
$url = parse_url(“http://net.tutsplus.com/articles/news/free-copies-of-pro-php-and-jquery?query=foo#bar”);
print_r($url);
Produces:
Array
(
[scheme] => http
[host] => net.tutsplus.com
[path] => /articles/news/free-copies-of-pro-php-and-jquery
[query] => query=foo
[fragment] => bar
)
My favourite so far. Thank you!
Didn’t know this till about a year ago – You can leave the trailing “?>” off at the end of your php files.
The closing delimiter is optional, and removing it helps prevent unwanted white space at the end of files which can cause problems elsewhere.
Keep the PHP separated of the HTML. It means a cleaner code and a a more efficient files organization in your proyect.
use “foreach” to go through arrays
Wrong, you should use while(list($key) = each($array)) { } because its faster:
http://www.phpbench.com/ (middle of the page; ‘Modify loop’)
Don’t write a complicated script to add commas to an integer. Just use number_format(). http://php.net/manual/en/function.number-format.php
The first thing I learned with PHP way back in the day was using includes for header/footer/nav, but learning how to pull strings from URL’s to custom tailor content on each page was a biggie for me.
Adding and subtracting dates with strtotime, example:
- date(“Y-m-d H:i:s”,strtotime(“+1 months”));
- date(“Y-m-d H:i:s”,strtotime(“-1 months”));
Regards
Use frameworks!! Like CakePHP, Zend or Drupal..!! It will bring you some headaches, but will remove a lot of them!
I like the print statement for debugging; also to use a variable with the host name for images paths was an epiphany at the time.
One really good tip is that
is the same that
(:
[]‘s
Rochester
Use a great editor/IDE.
For the Mac, you should use TextMate or Coda.
It will help you a lot, when developing and debugging.
NetBeans (http://netbeans.org/) is my IDE of choice. Eclipse is also pretty good. Both are Java-based, but I never feel like either is a huge drag on my system.
When referencing a URL-parsed variable (ie. ?foo=a) set an array of possible options for $foo and make sure your script first checks that the requested $foo is in fact; valid.
Use a IDE with syntax-highlighting and code-hints! It’s worth the time you’ll spend to find the one you like – it will make you write your code faster and make it way easier to debug..!
Remembering to turn on PHP-errors is quite helpful too!
Check php.net regularly. Theres hundreds of functions there waiting to be learned and used. It gets you to try some code, but more important it makes you to read code, a vital thing to be a good programmer.
Keep it simple, well-commented and organized.
Always use comments!
[My code wasn't submitted]
One really good tip is that
>?=$var ?>
is the same that
>?php
echo $var;
?>
(:
[]‘s
Rochester
Use PDO instead of mysqli because it makes switching databases easier without a rewrite of code.
Working with CakePHP helped me to learn more about OO PHP and its structure. As a side tip: Aptana + PHP plugin is very nice and it’ll run on multiple platforms (Windows, OSX, Linux, etc.)