» PHP.JS Namespaced
134 PHP equivalents
- PHP.JS Licensing
- PHP.JS SVN
- » PHP.JS Namespaced
- abs
- addslashes
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff
- array_diff_assoc
- array_diff_key
- array_fill
- array_flip
- array_keys
- array_key_exists
- array_map
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_reverse
- array_search
- array_shift
- array_sum
- array_unique
- array_unshift
- array_values
- base64_decode
- base64_encode
- basename
- bin2hex
- call_user_func_array
- checkdate
- chr
- compact
- count
- count_chars
- crc32
- date
- defined
- dirname
- echo
- empty
- end
- explode
- file
- file_get_contents
- floatval
- function_exists
- get_class
- get_included_files
- htmlentities
- html_entity_decode
- http_build_query
- implode
- include
- include_once
- intval
- in_array
- ip2long
- isset
- is_array
- is_null
- is_numeric
- is_object
- is_string
- join
- levenshtein
- long2ip
- ltrim
- md5
- md5_file
- mktime
- nl2br
- number_format
- ord
- parse_str
- preg_quote
- printf
- print_r
- rand
- range
- require
- require_once
- reset
- round
- rtrim
- serialize
- setcookie
- sha1
- sha1_file
- shuffle
- sizeof
- soundex
- split
- sprintf
- strcasecmp
- strchr
- strcmp
- stripos
- stripslashes
- strip_tags
- stristr
- strlen
- strnatcmp
- strpbrk
- strpos
- strrev
- strripos
- strrpos
- strstr
- strtolower
- strtoupper
- str_ireplace
- str_pad
- str_repeat
- str_replace
- str_rot13
- str_split
- substr
- substr_count
- trim
- ucfirst
- ucwords
- unserialize
- urldecode
- urlencode
- utf8_decode
- utf8_encode
- var_export
- wordwrap
PHP to Javascript Project: php.js
This article is part of the 'Porting PHP to Javascript' Project, which aims to decrease the gap between developing for PHP & Javascript.
A lot of people are familiar with PHP's functions, and though Javascript functions are often quite similar, some functions may be missing or addressed differently. The Javascript implementations should be as compliant with the PHP versions as possible, a good indication is that the PHP function manual could also apply to the Javascript version.
Porting crucial PHP functions to Javascript can be fun & useful. Currently some PHP functions have been added, but readers are encouraged to contribute and improve functions by adding comments. Eventually the goal is to save all the functions in one php.js file and make it publicly available for your coding pleasure.
If you choose to contribute, let me know how you want to be credited in the function's comments. You may also want to subscribe to RSS so you receive updates whenever new functions are posted.
Thanks to a lot of extra effort by Michael White (http://crestidg.com) there now is a namespaced version of PHP.JS available for your coding pleasure.
Namespaces
Namespaces are used to group functions & objects together. In this case, all PHP.JS functions are contained in one big object: PHP_JS.
Look at this code (not namespaced):
// call a PHPJS function p = strpos('Kevin van Zonneveld', 'e'); // call another PHPJS function d = date('F j, Y, g:i a');
Easy typing right? Just like you know from PHP.
Now look at this code (namespaced):
// create PHPJS object window.$P = PHP_JS(); // call a PHPJS function p = $P.strpos('Kevin van Zonneveld', 'e'); // call another PHPJS function d = $P.date('F j, Y, g:i a');
Little bit different. All PHP.JS code is now stored in it's own object. Some people like that because it can then never conflict with any existing function names (either by you, your CMS, other libraries or even JavaScript itself). This becomes clear with abs:
a = Math.abs(-7.25); // Javascript's own abs() function a = $P.abs(-7.25); // php.js's abs() function a = abs(-7.25); // only recognized when using normal php.js
Other namespacing benefits
Namespacing makes PHP.JS more flexible and makes it easier for developers to extend and include this library. They have more control over this project because it listens to a single name. If you want to dig a little deeper, try wiki.
Unnamespaced PHP.JS
Both unnamespaced & namespaced versions will be maintained and updated.
Why? I know that there are a lot of people out there who do not want to switch to a namespaced variant because they just want to be able to call the functions directly. For the ease of typing, but also because it resembles PHP, and that's the whole point of this project, right?
Download php.js
You can do that on every function's page. But for your convenience:
- uncompressed source: php.namespaced.js (186.5kB)
- minified: php.namespaced.min.js (18kB with mod_deflate enabled)
- compressed: php.namespaced.packed.js (37.4kB)
Future features
We're currently working on two major project improvements.
Better Versioning
All functions are versioned, but we're working on solid versioning of the main php.js file as well, this feature will be ready really soon and announced here.
Lightweight
PHP.JS contains a lot of functions and you probably won't use them all in one site. So to people who are concerned with things like bandwidth I can recommend a couple of things:
- Try the minified version & mod_deflate
The combination will shrink the total php.js to 18kB - Only copy-paste the functions you need
The last suggestion is not very developer-friendly though. You will survive but still.. We've got the idea to create an online application with a lot of check boxes and a couple of default presets. To allow you to exclude all kinds of functions and totally customize your own PHP.JS. Your unique combination of functions will additionally generate a hash, so you can always enter that hash again to download the newest versions of your favorite functions.
Dedicated site
We're currently working on a dedicated site for PHP.JS. Moving this project away from my Blog will make it more accessible and help professionalize the project.
New to PHP.JS?
If you're new to PHP.JS, checkout an example. We are not trying to port or emulate the entire language or control structures of PHP. We don't see the need because JavaScript seems to have more elegant features in that category anyway.
However in our eyes, PHP does provide a large set of standard functions that make developing very easy, and some of them don't have good standard JavaScript implementations, though they often would be great to have client-side.
So in this project by also providing the functions separately, we hope to keep people from inventing the wheel and give them a head start.
About the project
The PHP.JS project started in December 2007. It quickly became populair and within 4 months PHP.JS supported over 120 PHP functions all translated to JavaScript altenatives. PHP.JS is open source and has received (and still is receiving) code contributions from 50+ developers around the world, who have all helped to make PHP.JS what it is today. A rich Javascript library that adds functionality & PHP compatibility to your projects.
Like this article?
|
Then Dzone it! Or use another bookmark button below to show your support & help me spread the word. |
Hot StuffFlaming articles» Survive heavy traffic with you... | RelatedArticles like this one» PHP.JS Licensing |
tags: programming, javascript, php, phpjs
category: Programming - Javascript - PHP equivalents
read: 2,699 times






tagcloud
#7. Sincklation on 17 April 2008
#6. Kevin on 08 April 2008
#5. Weston Ruter on 08 April 2008
#4. Kevin on 02 April 2008
#3. _argos (waldo malqui silva) on 01 April 2008
#2. Dharmavirsinh Jhala on 29 March 2008
PHP.JS has been part of my common javascript library and now it has namespace too..
http://blogs.digitss.com
#1. BTM on 29 March 2008