Learning PHP – Things to Remember
As I continue on with Drupal I’m also beginning to digest PHP. I’ve worked with just about every programming language there is: Java, C, C++, Cold Fusion, Python, Perl, and even Fortran. I find scripting languages an excellent choice for web programming. However there is always a few tricks that you need to know – and these are easy to forget. I’m going to use this post as a place to “remember” these. Once I get enough I’m going to turn this into a page. If you have any tricks you’d like to contribute, just post a comment.
Things To Remember with PHP 5:
- Use isset() to test if a variable has a value.
- variable names are case sensitive; functions and stuff are not
- PHP is cool; Perl is not {just checking to see if you are paying attention :-)}
- When working with numbers watch out for wacky type conversion issues between types. Integers over about 2 billion are converted over to floats automatically. Booleans that are true convert to 1 but false converts to an empty string. Use type casting.
- Variable variables are interesting (kind of like c pointers). As with c pointers, if you don’t know what you’re doing then do not use them.
- Superglobal Arrays are incredibly useful, but also dangerous and a source of too much pasta in your programming diet.
- Objects are always passed by reference.
- Function arguments are pass by value with “$arg1” notation; to pass by reference use “&$arg1” notation.
- Use PDO for database connectivity.
- The foreach loop is powerful for iterating over arrays
- There are a number of very powerful array functions that are especially suited to database applications.
- Understand how things work before you use them not after – for example if you use XAMPP make sure you know the differences between their PHP config and the default from php.net because they are not the same.
- The ending “%>” is not required anymore…
- Control your extensions in php.ini and don’t have more than you really need.
- Regular Expressions are a key to high performance PHP applications.
Finally – these are fantastic PHP links to use for learning:
- http://hudzilla.org/phpwiki/
- 3-tier Development with PHP
- PHP Manual
- PHP Function Quick Reference
- PDO MySQL Introduction ; PDO Tutorial
- Using PDO Objects in PHP 5
- Alejandro Gervasio has a ton of short and well done articles on DevShed
I’ll update this as I continue to learn more about PHP. My main focus will be on developing database applications and looking at application architectures.
Some good tips here. Do you take requests?
Would like to see a tutorial on PHP + WSO2!
You read my mind! Now that I have a decent MySQL programming tool – see my latest entry – I’ll be cranking on a PHP-WSO2 Mashup test. My goal will be to create a mashup of all the wacky things I do so that I can keep track of it all. Boards, blogs, mailing lists, and stuff.
beren
If you’ve had issues with bool casting etc you might want to use === instead of ==. That’s a common mistake I’ve seen in the past.