Porting PHP to JavaScript

January 3, 2008

News

“Kevin van Zonneveld”:http://kevin.vanzonneveld.net/techblog/ has a great series of posts and JavaScript code snippets that give you JavaScript equivalents of some of his favorite PHP. The good news is there is some useful code here, the bad news is that the JavaScript code doesn’t support namespaces.

So far has has posted the following:

*(disc) “chr”:http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_chr/
* “ord”:http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_ord/
* “is_array”:http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_is_array/
* “base64_decode”:http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_base64_decode/
* “base64_encode”:http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_base64_encode/
* “trim”:http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_trim/
* “function_exists”:http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_function_exists/
* “basename”:http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_basename/

So if you work extensively with PHP and JavaScript, you will want to check out this series of short posts.

About Cal Evans

Many moons ago, at the tender age of 14, Cal touched his first computer. (We're using the term "computer" loosely here, it was a TRS-80 Model 1) Since then his life has never been the same. He graduated from TRS-80s to Commodores and eventually to IBM PC's. For the past 10 years Cal has worked with PHP and MySQL on Linux OSX, and when necessary, Windows. He has built on a variety of projects ranging in size from simple web pages to multi-million dollar web applications. When not banging his head on his monitor, attempting a blood sacrifice to get a particular piece of code working, he enjoys building and managing development teams using his widely imitated but never patented management style of "management by wandering around". Cal is currently based in Nashville, TN and is gainfully unemployed as the Chief Marketing Officer of Blue Parabola, LLC. Cal is happily married to wife 1.28, the lovely and talented Kathy. Together they have 2 kids who were both bright enough not to pursue a career in IT. Cal blogs at http://blog.calevans.com and is the founder and host of Day Camp 4 Developers

View all posts by Cal Evans

8 Responses to “Porting PHP to JavaScript”

  1. sevenforty Says:

    Other than from a hobby point of view, what is the need?

  2. kevinvz Says:

    People who’s primary language is PHP and are most familiar with all of it’s wonderful functions, often really know how to build something in PHP, but sometimes the Javascript functions just aren’t there. In those cases it just saves a little time otherwise spent reinventing (or searching for) the wheel.

    And though indeed it is a hobby, functions like base64_decode, wordwrap, stripslashes, ucwords, md5, may all come in quite useful when developing applications with a lot of client/server interaction. Because a lot of server side intelligence can be ported to client side more easily, and with high traffic sites, that will definitely save you some volume & hardware.

    But hey, nobody is ordering anybody to contribute or even use it, but my guess is plenty developers will enjoy the final result of this project.

  3. sevenforty Says:

    Well, I will say it’s an interesting experiment and I by no means am advocating for the original author to stop. But for production value I really do not see a need. Passing off huge chunks of logic to the client is highly debatable for a lot of reasons for one. Second, with a solid background in PHP development (or any language for that matter), scripting JavaScript isn’t too terribly difficult. Any PHP developer who can’t handle JavaScript probably isn’t much of developer to begin with. Like any serious developer, you should have core programming concepts down already. After that, picking up new languages is a matter of getting used to syntax and a little reading.

  4. _____anonymous_____ Says:

    yes, javascript is not that hard to learn. but being an avid php devloper , there are certain functions that you use so often that there is a hole when you program in other languages. i, for one, cannot live without the explode/implode function. so, i just go to this guy’s site, and it is there!

    thanks for the work. i wrote a short html_entity_decode() function but i cant find where to contribute.

  5. _____anonymous_____ Says:

    Not the best example because explode & implode can easily be replaced by split and join. But what about functions like javascript md5, require_once, array_unshift, number_format, etc. Real handy to have clientside in my opinion.

  6. _____anonymous_____ Says:

    "The bad news is that the JavaScript code doesn’t support namespaces."

    Not anymore. It’s just been announced that from now on PHP.JS will support namespaces.
    http://kevin.vanzonneveld.net/techblog/article/phpjs_namespaced/

    Just wanted to share that with you!

  7. kevinvz Says:

    We’ve moved to a new home. Be sure to checkout 400+ PHP functions ported to JavaScript
    http://phpjs.org/functions/index