ready-to-use resources, tutorials, tips and many other web development related stuff
WARNING! You probably have JavaScript disabled. Some features of this site will not work properly unless JavaScript enabled.
www.webdevelopmentstuff.com changed to www.webdevstuff.com

Use PHP Functions in JavaScript

Are you experienced PHP programmer and would like to use favorite PHP functions in JavaScript too? Sounds interesting, doesn’t it? You wouldn’t have to learn new JavaScript objects and functions, get used to new syntax, just write what you already know and have tested. Put PHP directly into JavaScript code and don’t care! Well, but how?

Thank God and the group of PHP.JS developers the answer is here! The PHP.JS is an open source project in which they try to port PHP functions to JavaScript. By including the PHP.JS library in your own projects, you can use your favorite PHP functions client-side. No server component required. You can choose how to use PHP.JS as follows:

Options of PHP.JS Usage

  • Download the default or any other package
  • Customize, compile and download your own function’s selection package
  • Go to the Functions and just copy-paste it into your own library or HTML code

I personally prefer download one of the prepared packages or select which functions I need and create my own package. Then, all you have to do to have PHP functions working in a JavaScript code is trivial.

Three Steps of PHP.JS Incorporation

  • Download a package according to your needs
  • Link downloaded library into your HTML code
  • Use available PHP functions like JavaScript ones

One simple example is more than thousands words. I downloaded default minimal package and used one basic function as a demonstration. For more information about this package, list of its functions go here.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>PHP in JS</title>
<meta http-equiv="content-language" content="en">
<meta name="author" content="Teddy Cyber">
<meta name="author-url" content="https://www.webdevstuff.com">
<meta name="description" content="PHP.JS Test File">
<meta name="keywords" content="php, javascript">
<meta name="creation-date" content="06/18/2009">
<script type="text/javascript" src="../js/php.default.min.js"></script>
</head>
<body>
<script type="text/javascript">
    echo('Hello World!');
</script>  
</body>
</html>

Well, you can guess what output I got! Yes, very good known “Hello World!” appears in the browser and no errors detected. This package works perfectly and smoothly at the first stroke. I think most of you now know what to do more with PHP.JS. As mentioned earlier, you just select the package of desired functions, link it to your HTML code or into another .js library and enjoy you favorite PHP functions in JavaScript!

Finally, maybe I should notice that PHP.JS project is still under development, so be patient if some bug will appear. Nevertheless the current implementation of PHP functions into the JavaScript is highly usable and may speed up development for PHP developers who are increasingly confronted with client-side technology.

Comments are closed.

© 2008 - 2024, webdevstuff.com | Powered by Wordpress | Theme by Elegant Themes | Valid XHTML and CSS | Subscribe to RSS