Archive for the ‘PHP’ Category

OOP PHP: What is the Double Colon in Objects/Classes for?

Friday, January 16th, 2009

phpI happen to be the only PHP programmer that I know, so it’s hard to ask your buddy what the heck that :: is used for in some OO PHP programs.  I know you could post it on a forum and get the answer, but I never did.  And Google wasn’t really forth coming when you put :: in a search.  Finally I found it in the PHP documentation:  check out Scope Resolution Operator (::).  In short, it’s used to access Static or Constant members of a class.  Here’s a brief example:

(more…)

Popularity: 14%

PHP: Using Switch to test Multiple Conditions

Sunday, August 3rd, 2008

Ok, so maybe this is common knowledge, but I still felt a little clever when I figured out this trick.  The situation was that I wanted to test a bunch of different conditions to figure out which time category a date falls into.  It would be fairly simple to do with a bunch of if…then…else’s, but really ugly.  Here’s the trick I came up with…use switch(true), and then set up your cases to evaluate to true or false. Here’s some example code:

(more…)

Popularity: 8%

PHP: Proper Case Function for PHP Strings

Monday, June 30th, 2008

Yes, PHP does have a proper case function for strings: ucwords($string)

If you’re looking for the lower and upper case string functions…here you go:

strtolower($string);

strtoupper($string);

Why not a strtoproper(), just to keep it consistent? Well, consistency here wouldn’t exactly be consistent for PHP, would it ;-) ?

Popularity: 11%

PHP: What is the & Ampersand Preceding Variables

Friday, June 6th, 2008

Well, this one is hard to find on Google, so I’m putting it in WhyPad to make it easy to find…at least for me, and I guess for you too since you here. Welcome! You may see PHP code snippets (PHP 5+ only) that have an ampersand, ‘&’, preceding a variable like &$my_variable. So, what does it do? It sets up a reference to the original variable instead of copying it’s value. The following snippet demonstrates: (more…)

Popularity: 38%

Codeigniter: ActiveRecord Join Tip

Wednesday, May 21st, 2008

I ran into a problem with a SQL JOIN in Codeigniter’s ActiveRecord class. This post will help you get past that, but first a little intro to frameworks. If you’re a PHP developer, you have probably considered one of the incredibly useful frameworks that are available today. Cake, Zend, Symfony, and Codeigniter are some of the biggest names in the PHP framework game today. I’ve tried both Cake and Codeigniter, and found Codeigniter less strict, so I’ve been gravitating towards it. If you haven’t tried a framework, I highly recommend doing so. Any of the four I’ve listed are excellent choices, each with strengths and weaknesses, and rabid fans who can take their zealotry to extremes at times. As with anything that requires a computer, there is always a learning curve when taking on a framework, whether it’s figuring out MVC or just learning the syntax and structure of the framework itself. But the payoff is tremendous amounts of functionality that you don’t have to code, somewhat enforced structure, and possibly even cleaner, more manageable code. Check out Codeigniter’s User’s Guide to give you some idea of its features (you’ll have to click the little black “Table of Contents” tab at the very top & right of the page…it’s sort of hidden). (more…)

Popularity: 22%