Beware: PHP Modifies Input Variable Names

Posted: March 15th, 2009 | Author: chris | Filed under: PHP | Tags: | No Comments »

PHP has no shortage of vestigial quirks left over from earlier versions. Some are just annoying and ugly. Others, such as register globals, are dangerous when turned on or not properly accounted for. One lesser known but potentially bug-inducing quirk is PHP’s automatic conversion of certain characters in the keys of $_GET, $_POST, $_REQUEST, and $_COOKIE to underscores.

Try this as an experiment. Enter the following into a PHP file in a location that is web accessible:

<?php
header('Content-type: text/plain');
var_dump($_GET);

Then enter the URL for the script into your web browser along with the following query string: x.y=z.

Here’s the output you’ll get:

array(1) {
  ["x_y"]=>
  string(1) "z"
}

Notice that the period we included in the query string parameter name was changed into an underscore!

Here’s what the PHP manual says about this behavior:

… it is important to note that PHP will automatically replace any dots in incoming variable names with underscores.

The manual mentions only dots, but PHP converts more than that. Here is a complete list of the characters that are converted to underscores:

Space ‘ ‘ 0x20
Dot ‘.’ 0x2e
Open Square Bracket ‘[’ 0x5b
Various non-printable 0x80-0x9f

While these characters are not commonly used in input variable names, the fact that they are converted is worth keeping in the back of your mind while you code in PHP.

http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/digg_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/furl_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/google_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png

7 Habits For Effective Text Editing 2.0

Posted: February 8th, 2009 | Author: chris | Filed under: VIM | Tags: , | No Comments »

http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/digg_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/furl_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/google_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png

Douglas Crockford on Ajax Performance

Posted: February 7th, 2009 | Author: chris | Filed under: Javascript | Tags: , , | No Comments »


Douglas Crockford: "Ajax Performance" @ Yahoo! Video

http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/digg_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/furl_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/google_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png

A Short GNU Screen Tutorial

Posted: January 24th, 2009 | Author: chris | Filed under: UNIX | Tags: , , | 2 Comments »

GNU Screen is a command-line application enabling you to run multiple virtual terminals within a single terminal. Essentially, Screen manages terminals and the applications running in them, not entirely unlike how a terminal emulator does with multiple tabs open.

So, what are the benefits of using Screen over the functionality in your terminal emulator?

  • Programs running under Screen continue to run even when the terminal running it is closed. For example, if you’re running Screen over SSH and the connection is interrupted, the terminals and programs managed by Screen continue to run. You simply need to reconnect and run screen -r to reattach to your previous Screen session and the programs running under it.
  • If you’re working on a remote server and need to shut down or restart your computer, you can do so without closing your remotely running programs. Screen will chug along on the server until you reconnect using the same or even a different computer and run screen -r.

Now that you know the benefits of Screen, how do you actually use it?

Screen’s configuration is kept in the file ~/.screenrc. Copy the following lines into that file:

startup_message off
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'

The first line turns off the annoying Screen start up message. The second and third lines add a status bar with window information and the date/time to the bottom of your terminal window.

To start screen, execute the following command:

screen

To resume a previous screen session, execute the following command:

screen -r

Here are the commands you need to know to use screen effectively:

Command Description
CTRL+a c Create a new window
CTRL+a n Switch to the next window
CTRL+a p Switch to the previous window
CTRL+a a Switch to the last active window
CTRL+a <DIGIT> Switch to window number <DIGIT>
CTRL+a A Enter a name for the current window
CTRL+a :number <DIGIT> Change the number of the current window to <DIGIT>
CTRL+a d Detach from screen

Here are a few other things you need to now:

  • To shut down screen, close all programs running under it. To close a shell, use the exit command.
  • If you have problems with the delete key, put the following line in your .bashrc: alias screen=’TERM=screen screen’ (this problem occurs on Ubuntu). Execute the following command to re-source your .bashrc file: . ~/.bashrc.
http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/digg_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/furl_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/google_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png

Search and Selectively Replace In Multiple Files Using VIM

Posted: January 10th, 2009 | Author: chris | Filed under: VIM | Tags: , , , | 1 Comment »

The UNIX command sed is great at doing regular expression-based search and replace within multiple files. However, since sed is not an interactive program, it’s not helpful in situations where control is needed over what gets replaced on a match-by-match basis. VIM to the rescue!

For the sake of example, assume that we have a directory of source files in which we would like to replace certain invocations of the function func1 with func2, and that there is no obvious way to tell using a regular expression which invocations need changing. To start, open all the source files in VIM:

vim *.php

Then, while in normal node, enter the following command:

:bufdo %s/func1/func2/gce | write

VIM proceeds to execute the search and replace in all currently opened buffers (files), prompting you to approve the replacement of each occurrence of func1 with func2. Here’s what each part of the above command does:

  • bufdo instructs VIM to execute the command in all opened buffers.
  • %s instructs VIM we are doing a search and replace across all lines (we could replace %s with 1,10s if we wanted to restrict the operation to the first ten lines of each file).
  • The g modifier instructs VIM to match all instances of func1 on a line, not just the first.
  • The c modifier instructs VIM to ask us whether or not to replace each instance of func1.
  • The e modifier instructs VIM to keep going even if an error occurs. This is needed to prevent the search and replace from aborting on a file that does not contain any instances of func1.
  • | instructs VIM that the part that follows is a separate command to execute after the part preceeding the |.
  • write instructs VIM to save the changes to the current buffer.

Hopefully, this command saves you a lot of tedious effort!

http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/digg_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/furl_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/google_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png

XML and XSL Folding In VIM

Posted: January 6th, 2009 | Author: chris | Filed under: VIM | Tags: , , , | 2 Comments »

Recently at work, I was assigned a project requiring me to comprehend several large XSL files. After struggling for a while, I realized that folding up most of the child xml nodes would make understanding the code much easier. It turns out, VIM can do this easily. Just add the following lines to your .vimrc file:

let g:xml_syntax_folding=1
au BufNewFile,BufRead *.xml,*.xsl setlocal foldmethod=syntax

Here’s a quick reference to get you up to speed with folding in case you’ve never used it before:

Command Description
zo Open the fold underneath the cursor.
zc Close the fold underneath the cursor.
zC Close the fold underneath the cursor and all folds within that fold recursively.
zM Close all folds.
zR Open all folds.

For more information about folding in VIM, type “:help folding”.

http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/digg_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/furl_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/google_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png

What is a Paamayim Nekudotayim?

Posted: December 3rd, 2008 | Author: chris | Filed under: PHP | Tags: | No Comments »

Have you ever gotten this error message from PHP?

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM

Internally, PHP refers to the scope resolution operator (::) as “Paamayim Nekudotayim”. Here’s an article explaining why it’s called that. In a nutshell, Zend is an Israeli company and “Paamayim Nekudotayim” is a Hebrew phrase meaning double colon.

http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/digg_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/furl_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/google_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png

Recording Macros In VIM

Posted: December 1st, 2008 | Author: chris | Filed under: VIM | Tags: , , , | No Comments »

When programming, situations frequently arise in which a large number of repetitive manipulations must be performed. Taking the obvious route and actually performing the manipulations outright may be faster in some situations, but in most you’ll save alot of time and effort by devising a more elegant solution. When using VIM, recording a macro is often the elegant solution.

Suppose that you have the following lines in a buffer:

this is a test
i love the vim editor
gorilla codes
all your base belong to us
hello world

Next, suppose that you need to perform the following manipulations on each line:

  1. Capitalize the first letter
  2. Add a comma at the end

Since the block of text contains only five lines, performing the manipulations manually is trivial. In fact, only six key presses per line are required. But, imagine if the block contained a thousand lines instead. You’d need to perform six thousand key presses to transform all the lines! There must be an easier way …

As mentioned above, when faced with the task of performing a large number of repetitive manipulations, recording a macro is often the most elegant and easiest solution.

To begin, position your cursor over the lower case ‘t’ character in the first line, and ensure that VIM is in normal mode (hit the escape key a few times if you’re unsure). Next, press the ‘q’ key twice. VIM is now recording a macro. Until you press the ‘q’ key again while in normal mode, all the keys that you press will be recorded and stored into the ‘q’ register. Note that all keys means all keys; if you mess up, you’ll need to start over or fix the macro before using it.

Since VIM is now recording key presses, you’ll need to perform the keystrokes that execute the two manipulations on the current (first) line. There are many ways to go about this. The most important thing to remember is that the manipulations must be carried out in a way that will correctly transform all of the lines that the macro will be applied to. For example, you can’t just press ‘Delete’ followed by ‘T’ to capitalize the first letter. That would limit your macro to working only with lines beginning with ‘t’! VIM needs instruction on how to captialize any line, not just those beginning with a particular letter.

While in normal mode, pressing the ‘v’ key followed by the ‘U’ key capitalizes the letter under the cursor. Further, it does so in a way that works on any letter, whether it is lower case or already upper case. Therefore, by entering ‘vU’, you instruct VIM to capitalize the first letter in a way that works on all possible lines. So, if you haven’t already, enter those two keys now. To complete the macro, enter the following sequence of characters:

  1. ‘A’ - to move the cursor to the end of the line and to put Vim into insert mode.
  2. ‘,’ - to insert a comma character.
  3. <ESC> - to put vim into normal mode.
  4. ‘j’ - to move the cursor down one line.
  5. ‘^’ - to move the cursor to the beginning of the current line.

To finish the macro, press the ‘q’ key. VIM is no longer recording your keystrokes. If you entered the sequence of keys correctly, the first line should have had the two manipulations performed on it. Additionally, the cursor should be on the first character of the second line.

Since the macro is now recorded, all that remains is executing it repeatedly until all the lines are transformed. To execute the macro once, press the ‘@’ key followed by the ‘q’ key. The two manipulations are performed on the second line, and the cursor is now on the third line. Next, press the ‘@’ key twice. The ‘@@’ command tells Vim to execute whatever macro it last performed. This command’s key sequence is convenient because it allows you to repeatedly press or hold down the ‘@’ key until all the lines are transformed.

While recording a macro does take some time, performing keystrokes manually in order to transform a large block of text is typically slower. Well, at least one thing is for certain: if you use a macro, your hands will be less tired when you’re done.

http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/digg_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/furl_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/google_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png

PHP Is Smarter Than You Think. Don’t Pass Function Arguments By Reference.

Posted: November 30th, 2008 | Author: chris | Filed under: PHP | Tags: , | 8 Comments »

Recently, I had the misfortune of digging through a substantial amount of poorly written, undocumented PHP code. It took several hours just to get a handle on it, and even after many more hours, I still didn’t understand all of it. One thing that made the code hard to understand was that nearly all of the functions’ arguments were passed by reference, apparently in an effort to improve memory efficiency and performance.

Passing arguments by reference in order to improve performance payed off in the PHP 4 era. However, enhancements made to PHP in version 5 render this technique completely unnecessary. There is no performance benefit in passing arguments by reference anymore. Further, as alluded to above, using references in this manner makes code harder to understand and less flexible.

Take a look at the following example:

function funcCopy($copyParam)
{
    echo "In funcCopy(): ", memory_get_usage(), "\n";
}
 
function funcRef(&$refParam)
{
    echo "In funcRef(): ", memory_get_usage(), "\n";
}
 
echo "Before bigArray is created: ", memory_get_usage(), "\n";
$bigArray = array_fill(0, 10000, '-');
echo "After bigArray is created: ", memory_get_usage(), "\n";
 
funcCopy($bigArray);
funcRef($bigArray);

Tracing through the code from a high level perspective, the following observations could be made:

  • When funcCopy is called, $bigArray is copied into a local variable named $copyParam.
  • When funcRef is called, $refParam references the same array as $bigArray.
  • Conceptually, $bigArray and $copyParam are different arrays, while $bigArray and $refParam are the same arrays.

While these are true statements, they are only true from a high level language perspective. Under the hood, PHP allocates memory in a more intelligent manner. Internally, PHP employs copy-on-write, an optimization strategy frequently used in computer science to delay or prevent resource duplication. While executing functions, PHP uses the copy-on-write strategy in order to avoid duplicating variables passed as arguments until they are modified. If an argument is never modified inside a function, then the original variable is never duplicated (important: if the argument is modified or copied, then a copy will be made … so be mindful of what you are doing). Employing this strategy doesn’t affect program correctness, and it saves memory and CPU time when arguments are not modified inside a function (which is most of the time). In terms of the example above, this means that when funcCopy is called, the amount of memory allocated by PHP should not increase significantly, since $copyParam is not modified or copied inside funcCopy.

Indeed, running the code above using PHP 5.2.6 yields the following output:

Before bigArray is created: 54216
After bigArray is created: 600376
In funcCopy(): 600504
In funcRef(): 600528

Notice that the amount of memory used by PHP increased significantly only after $bigArray was created. Calling funcCopy with $bigArray as an argument increased memory usage by only around a hundred bytes. Further, calling funcRef with $bigArray increased memory usage by about the same amount that calling funcCopy did. So, as you can see, passing arguments by reference yields no performance benefit.

Beyond the fact that passing arguments by reference gives no performance benefit, why should extra care be taken to avoid them? Take the following function as an example:

function addStuffUp(&$a)
{
    return $a[0]+$a[1]+$a[2];
}

Looking at just the function’s signature, you’re lead to believe that the argument passed in is modified by the function. Only by looking at the function’s definition do you learn otherwise. You might think that putting a comment on top of the function would clear up the problem. But, since PHP does not enforce the correctness of comments, someone could come along and change the function so that $a is modified. That would likely introduce hard to find bugs.

In addition to making code harder to understand and easier to mess up, passing arguments by reference is bad because it reduces flexibility. For example, calling addStuffUp like this:

addStuffUp(array(1,2,3));

causes the following error:

Fatal error: Only variables can be passed by reference

Why? Because PHP expects the argument passed into addStuffUp to be something it can modify. You’d have to split the code into two lines in order to correct the error:

$x = array(1,2,3);
addStuffUp($x);

Clearly, that’s not as nice as the first call; a variable and an extra line are introduced unnecessarily.

Hopefully, I’ve convinced you that it’s best to avoid passing arguments by reference. Ideally, formal parameters should include a reference only when a function actually needs to modify a variable. And even then, it’s probably worth refactoring to avoid the reference completely.

http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/digg_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/furl_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/google_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png

5 Things About VIM I Wish I Learned Sooner

Posted: November 28th, 2008 | Author: chris | Filed under: VIM | Tags: , , | 7 Comments »

Hopefully you’ll have at least one head slapping moment while reading this list. Learning any of these things earlier would have saved me a boat load of time.

  1. The clipboard is accessible via the + register - If you’re using VIM on a local machine, you can access the clipboard via the plus (+) register. For example, while in normal mode, “+yy copies the current line into the clipboard, and “+p outputs the contents of the clipboard into the current buffer.

  2. * finds the next occurrence of a word - Pressing * on a word while in normal mode makes the cursor jump to the next occurrence of the word underneath the cursor.

  3. VIM comes with a default .vimrc that rocks - On Ubuntu 8.10, it’s located at the path /usr/share/vim/vimcurrent/vimrc_example.vim. Using that file to start with makes a lot more sense than trying to figure out all the typical settings yourself.

  4. :r /path/to/file outputs a file into the current buffer - While in normal mode, entering :r /path/to/file outputs the specified file into the current buffer. Now, anytime you find yourself typing something repeatedly, save it into a file somewhere and use the :r command to avoid typing it again.

  5. Enter visual line mode with V, enter visual block mode with CTRL+V - While in normal mode, pressing V puts VIM into visual line mode. It differs from visual mode in that VIM will select whole lines at once, instead of just single characters. While coding, visual line mode is more useful than visual mode (how often do you select anything but whole lines?). Similarly, while in normal mode, pressing CTRL+V puts VIM into visual block mode. It allows you to select a rectangular block of characters starting from any row or column. You probably won’t use it too often, but when you need it, you really need it.

http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/digg_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/furl_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/google_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://lolcodrz.com/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png