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 http://phpjs.org/, 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: http://phpjs.org/functions/.

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) {
  // http://kevin.vanzonneveld.net
  // +  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;
}

Summary

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.

thank_you.js

Web and Mobile Software Developer, with years of experience. My biggest passions are programming, new technologies, e-commerce, as well sports, cars, and of course my family.

Facebook Twitter LinkedIn Google+ Skype Xing 

Share: Share on Facebook2Tweet about this on TwitterShare on Google+1Share on LinkedIn3Share on Tumblr0Digg thisEmail this to someonePin on Pinterest1
Both comments and pings are currently closed.