Porting PHP to JavaScript
“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.


8 comments to “Porting PHP to JavaScript”
January 4th, 2008 at 8:34 pm
Other than from a hobby point of view, what is the need?
January 6th, 2008 at 1:52 pm
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.
January 8th, 2008 at 5:05 am
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.
January 10th, 2008 at 8:23 pm
http://hexmen.com/blog/2007/03/printf-sprintf/
March 15th, 2008 at 5:22 am
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.
March 29th, 2008 at 8:11 am
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.
March 29th, 2008 at 4:08 pm
"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!
May 28th, 2009 at 9:31 am
We’ve moved to a new home. Be sure to checkout 400+ PHP functions ported to JavaScript
http://phpjs.org/functions/index