Decaflon

Welcome to Decaflon! Where the geeks hang out: Signup or Login Here
Decaflon is proudly hosted by (mt) Media Temple.  We recommend them for your web hosting needs.
Clips: Popular Clips Upcoming Clips Notes: All Notes

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?

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().

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.

I use the regular expression functions a lot, favorite being preg_replace()

username Zoom

Hal

Written Feb. 6, 2007 / Report /

I really like print_r()

I second preg_replace

I also really like round()

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.

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.

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 .. :)

I do like extract for templating...

strtotime() is quite juicy. That...and urlencode(). :)

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 ;-).

username Zoom

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

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).

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 ...

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()

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. :-/

strpos() is one of my favorites...

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...

ob_start() for buffering output... always super useful.

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.

array_keys and in_array + foreach :)

I only just found number_format() which is blooming good for adding 2 decimal places to a price. Great when coding an online store.

I like shorthand if statements 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 traditional if {} 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.

Don't forget the trusty:

nl2br()

for doing quick and easy formatting of line breaks without HTML!

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";
}

I will third strtotime() as a lifesaver, especially when dealing with fickle old MySQL.

However, stripslashes() is likely my most abused function.

Please Login To Leave A Comment

Decaflon Sponsors Get in touch if you want in.

Hot Notes (View all »)

 

Decaflon is part of the Chawlk Network of sites.

9 Great Places To Visit, Hang Out, & Meet New People

What's new and interesting at other Chawlk Network sites: