scriptNode

Web development with a focus on JavaScript RSS

Common PHP Equivalents in JavaScript

Intermediate Matt Hackett Published January 3rd, 2009 by Matt Hackett

It's pretty widely known in the web development industry that server-side scripting is important, and PHP is an extremely common choice. Indeed, for many programmers it's even their first language. It's quite common for web devs to begin on the server, then later require some client-side interaction, at which point they must turn to JavaScript.

I often see questions similar to, "I've got these few lines of PHP code, how would I do this in JavaScript?" I've actually already answered a question like this, but I kept getting more questions, so I thought I'd cover many at once:

Pushing to an array

Pushing a new value to the end of an array is a common, trivial task. Here's some PHP to accomplish this:

Also note this quick pointer from php.net's array_push() documentation:

If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

This isn't the case in JavaScript. The native Array.push method is most optimal:

die() or exit

There has been much confusion regarding what JavaScript is and how it works in the browser. When looking for an equivalent to exit, one probably doesn't understand that client-side JavaScript is a behavioral language that's intended to react to events. In PHP, a script executes, stopping only when told to do so, and then it's done. JavaScript lives and breathes on the page, reacting to the mouse being moved, keys being pressed and even time changing.

So it's important to understand why this is not a fair equivalent, and that even asking for one shows a misunderstanding. That said, a bit of procedural JavaScript can be exited by using an anonymous function. Here's some example PHP and then similar code in JavaScript:

While the language behavior is not one-to-one, taking advantage of JavaScript's closure already has advantages, among them being able to easily exit a function via return.

include or require

There have been some arguments about this equivalence in JavaScript. Just like the previous example, this equivalence is not one-to-one and should be thought about in very different terms. That said, here is a quick example of including a PHP file within a PHP script:

To include an external script in JavaScript, writing a method or including a library is definitely best. I wrote about this once, but there are libraries such as YUI that have better support with more options. Here's what this would look like using YUI:

Redirection

This one is easy and straightforward. A redirect is accomplished with PHP's header() function:

Also very simple in JavaScript:

Static classes

In PHP, it's a pretty common programming pattern to use a static class for encapsulation, sort of namespacing some methods and properties. Here's an example:

For this JavaScript equivalence, it probably makes the most sense to use an object literal:

String manipulation

String manipulation is absolutely mandatory in most applications. Fortunately, it's a breeze thanks to PHP's fantastic string functions:

Each of these have perfect equivalents except for trim(), which we need to implement ourselves:

Or, we can augment the String global so that strings will have this method natively:

While not all of these equivalents are smooth (or even recommended), it's typically helpful when learning a new language to have it compared to a language one is already familiar with. If you can think of any other common PHP tasks that have relatively similar equivalents in JavaScript, please let me know in the comments.

Read other articles tagged: , , ,

Comments (3)

  • Jani Hartikainen

    There’s also a library called PHP.JS, which ports various PHP functions to JS

    http://kevin.vanzonneveld.net/techblog/article/phpjs_licensing/

    I haven’t yet decided whether I like the idea or not =)

  • Wally

    I prefer (and a Raptr standard):

    $a[] = $foo, instead of array_push($a,$foo) cause it’s prettier. didn’t know there was cost to calling array_push; good to know.

    within JS, array.push(foo) is also more readable than array[array.length] = foo;

    within the front-end, i’m not convinced of using exit or die() in PHP or ‘return’ within JS context ….

    i’m sure we’ll have many philosophical discussions on software design at Raptr =)

  • Matt Hackett

    @Wally: ironically, I found the “bad” method being used in the Raptr code ;)

Thoughts?

(required)

(not shared)

© 2010 scriptNode