Back in 2007 I wrote an article titled PHP vs Ruby – Practical Language Differences which drew a fair amount of attention. Now that I’ve been working with Ruby in much more depth and both PHP and Ruby have matured dramatically over the past five years it is time to reevaluate the comparison.
Run PHP Script in Vim
When developing PHP apps I often want to run the current file and see the results. Sometimes I even want to save those results, or perhaps manipulate the results. So I wrote this little vim function to run my currently open PHP file and place the results in a new VIm buffer.
My Favorite Vim Tips
Here are some of my favorite tips for working in Vim. I use MacVim but everything below should apply to any version of Vim you are using. Vim is such an amazingly powerful editor you can use it for years and still learn new stuff all the time. This is a small collection of features I use most often and find most helpful. Hopefully they will be helpful to you as well.
camelCase to snake_case
Here is a TextMate command that will convert all of the selected text from camelCase to snake_case. This command is specifically designed for PHP. Thanks to PHP not having namespaces (until recently) many developers use PEAR naming conventions resulting in code that has class names like BP_Common::fancy_function(). This command will not convert the BP_Common.
Reality66 Naming Conventions
Here are the coding standards we use at Reality66. The purpose of these naming conventions and standards is to provide an easy to remember set of rules so all Reality66 developers write consistent code without having to constantly reference coding standards documentation. The primary intent is to have a small, easily memorized set of rules for each coding context.
How To Match A Float Column In MySQL
The Float datatype in MySQL is inherently inaccurate. If you are planning to use a float datatype for a column in your database you should reconsider, especially if you are planning to use it to store money values. Here is an example of the problem.
If Statement Variable Scope In PHP
Variable scope is the context within your code in which a variable is defined and able to accessed. If you try to access a variable that is out of scope, the variable will be undefined and you will not get the results you are expecting.
In PHP, variables all exist within the same scope when your code is inline or included in an include or require statement. Classes and functions have their own scope. If statements do not have their own variable scope.
WordPress register_activation_hook not firing
The register_activation_hook() in WordPress looks for the “wp-content/plugins” directory in the plug-in file’s canonical pathname. So, if your files physically live somewhere other than in your WordPress tree, WordPress (PHP) calculates inappropriate paths.
The Solution: Move all your plug-in files into your WordPress tree and the activation hook should start firing.
Sort Text Column Numerically in MySQL
Normally when you want to sort numerically on a column in your database you’d make the column some sort of numeric type such as an int. Sometimes, however, you are stuck with someone else’s schema and they have decided to store numbers in a text type column and you need to sort your results numerically because an alphabetic sort on a number does not produce the results you want. There is a quick trick to that makes this easy.
John 3:16 In PHP and Ruby
John 3:16 in PHP
1 2 3 4 5 6 |
$son = $god->loved($the_world); foreach($whosoever as $person) { if($person->believe($son)) { $son->everlasting_life($person); } } |
John 3:16 in Ruby
1 2 3 4 |
son = god.loved(the_world) whosoever.each do |person| son.everlasting_life!(person) if person.believe?(son) end |