I've been writing a whole lot of PHP lately and one of the things I like most about the language is the amount of built-in functions it includes, many more than other languages. After having to write most things from scratch in Java, coming over to PHP is fantastic.
Recently I've been digging the ucwords() function which capitalizes the first letter of each word in a string. Very useful :)
What are your favorite PHP programming constructs, or if not PHP, then your favorite functions from the programming language you work most in?

26 Comments
chapstick
Written Feb. 6, 2007 / Report /
Ah man, there's too many to list. In my site I've used
str_replace()a lot to help turn my article titles into a url friendly string.Actually I love all the string manipulation you can do with PHP, including
ucwords().Nicole
Written Feb. 6, 2007 / Report /
I'm a huge fan of
var_dump(). And now I get angry whenever I program in a language that doesn't have a similar function.karmadude
Written Feb. 6, 2007 / Report /
I use the regular expression functions a lot, favorite being
preg_replace()Hal
Written Feb. 6, 2007 / Report /
I really like print_r()
seopher
Written Feb. 6, 2007 / Report /
I second
preg_replaceI also really like
round()jwynia
Written Feb. 6, 2007 / Report /
strtotime is what I've often called the most "magical" function in all of PHP. You pass it a plain English description of a date or time and it gives you a timestamp.
Given how big of a pain it can be to work with dates, being able to say strtotime("yesterday") or strtotime("next week") or strtotime("-4 days") and you get the appropriate time is very useful in quick scripts.
If precision matters, you should still do the manual calculations, but I still use it all the time.
ConnorWilson
Written Feb. 7, 2007 / Report /
I also like
var_dump(), it's great for sessions and not forcing you to remember everything in a session or w/e, plus it helps with learning things like the phpBB code.imajed
Written Feb. 7, 2007 / Report /
i want to say something related with : ucwords , i do not know when you use this function and where , but it's a presntation function , and i prefer if you use css function to do something like this , it's not good to change a string to do visual thing because php is a programming langauge not visual one .. :)
about my fav function is : eval and extract .. :)
MikeP_
Written Feb. 8, 2007 / Report /
I do like extract for templating...
Shpigford
Written Feb. 8, 2007 / Report /
strtotime() is quite juicy. That...and urlencode(). :)
bbenzinger
Written Feb. 8, 2007 / Report /
One would have to be time(). I use it often for unique id's mixed with md5. Also for checking page load time, setting cookies life span, database timestamps, temp file names, and so on. Pretty handly, little function.
Lastly, I'd have to say.. the header() function. I change the header location a lot after receiving post data. Also, when dynamically creating documents and files... like making an RSS feed, generating images, dynamic js files, and passing multiple files as a zip.
I also like explode(), just for its name really, and because it's useful at times for splitting up data and requests ;-).
Oli
Written Feb. 8, 2007 / Report /
I really love do_you_think_they_could_have_made_this_function_name_any_longer()
The PHP function "structure" sucks. You guys need some form of packaging to keep relevant things together.
ASP.Net uber alles
Craige
Written Feb. 10, 2007 / Report /
imajed: PHP is not only used for the web. I have used it a number of times for my desktop: a cases where CSS is not applicable. You are right though when it comes to the web.
Oli: Yes, PHP doesn't have the best packaging structure; agreed. However I am hoping they choose to include namespaces in PHP 6 like they were talking. If done right, could easily cut down on all PHP functions being in the global namespace, and thus reducing naming conflicts (and increasing speed).
imajed
Written Feb. 14, 2007 / Report /
craige : good point craige
Oli : I'm php programmer and also c#.net :) i work with asp.net last 3 months and with my experience with php .. i can say , php need a lot for work to get what asp.net have.. one thing asp.net needed and it will be unbelievable , comparing with php , the code in php under your hand , you know every thing , and all thing , in asp.net , you do not know what's happen some times .. you do not understand what's the problem ...
jmathias
Written Feb. 14, 2007 / Report /
I'm a fan of in_array(), I also enjoy on occasion substr(), although my favorite and really not sure where I'd be without it is *_query()
Paul
Written Feb. 14, 2007 / Report /
wow, we gotta make a list of these handy functions. So many of these would come in handy. I tried to write my own ucwords() until I found out it existed about 2 weeks ago. :-/
Bryan
Written Feb. 14, 2007 / Report /
strpos()is one of my favorites...MikeP_
Written Feb. 14, 2007 / Report /
wow, we gotta make a list of these handy functions.
You're in luck, Paul, someone already did it for you! :P
http://www.php.net/manual/en/funcref.php
You might also want to click thru here:
http://hudzilla.org/phpwiki/index.php?title=Main_Page
Some beginners or people new to PHP find that an easy thing to click thru and get to know what it offers...
montoya
Written Feb. 16, 2007 / Report /
ob_start()for buffering output... always super useful.eternalsword
Written Feb. 16, 2007 / Report /
I think include() is one of the most useful functions especially when creating templates.
If for some reason, I don't have access to database facilities, I find that file_exists() is really useful for detecting timestamped content.
m3nt0r
Written Feb. 16, 2007 / Report /
array_keys and in_array + foreach :)
andypearson
Written Feb. 16, 2007 / Report /
I only just found
number_format()which is blooming good for adding 2 decimal places to a price. Great when coding an online store.jonathand
Written Feb. 21, 2007 / Report /
I like shorthand
ifstatements for simple tests (useful in PHP and JavaScript actually). It can greatly streamline code though it may take getting used to if you're used to the traditionalif {} else {}block.So instead of:
if (!empty($var)) { print "$var"; } else { print "empty"; }You could use:
print (!empty($var)) ? $var:"empty";A little easier on the eyes when staring at line upon line of code.
rickcurran
Written Feb. 22, 2007 / Report /
Don't forget the trusty:
nl2br()for doing quick and easy formatting of line breaks without HTML!
Ivan
Written Feb. 22, 2007 / Report /
I tend to use "dd()" an awful lot. It's just my own little wrapper for "print_r()" and it's very useful for outputting variables directly into the HTML source, so it's invisible unless you "view source" in the browser.
function dd($var, $cap = 'var') {
echo "<!-- $cap = ";
print_r($var);
echo " -->\n";
}
evhan
Written Oct. 15, 2007 / Report /
I will third strtotime() as a lifesaver, especially when dealing with fickle old MySQL.
However, stripslashes() is likely my most abused function.