PHP functions in JavaScript – the php.js library

JavaScript file

Level: intermediate

In this short article we want to present a very interesting library – php.js.

PHP functions in JavaScript thanks to the php.js library

The library allows the programmer to use the functions known from PHP in JavaScript, by implementing their capabilities and syntax. Of course it’s about equivalents of PHP functions, implemented in JavaScript.

The library is available at, and also on github.

This solution may be useful in many ways.

First of all, this may facilitate the migration of ready algorithms or codes written in PHP. In this case the time of (re)implementation will be much shorter.

We can also use this solution for convenience. For example PHP provides very useful and great in use function: date (date and time formatting).

Working with dates and time in JavaScript may be not so cool, so the way is to use the external library. And it may be php.js, which provides an equivalent of date() in JavaScript.

In addition php.js may simplify programming in JavaScript for developers well familiar with PHP, and not so well with JavaScript.

The full list of functions we can find here:

By analyzing some implementations for sure we will get interesting, and maybe even beautiful examples of JavaScript code.

For example the implementation of in_array() function, is a nice piece of JS code:

function in_array (needle, haystack, argStrict) {
  // +  original by: Kevin van Zonneveld ...
  // ...

  var key = '',
    strict = !! argStrict;

  if (strict) {
    for (key in haystack) {
      if (haystack[key] === needle) {
        return true;
  } else {
    for (key in haystack) {
      if (haystack[key] == needle) {
        return true;

  return false;


As we can see php.js library has various advantages, and one of them can include interesting and clever code examples in JavaScript. They can teach us something new about the language.


Both comments and pings are currently closed.

One Response to “PHP functions in JavaScript – the php.js library”