Use PHP functions in JavaScript

Contact

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Louis Stowasser
Aug 14th Permalink

q  I'm planning to implement the session functions but was wondering if the functions are allowed to call other functions in PHPJS (such as uniqid(), setcookie(), etc etc).

Gravatar
Brett Zamir
Aug 3rd Permalink

q  Yes, you have understood correctly. Pretty much just the PHP function API made usable in JavaScript to allow people to get as close an equivalent as possible with functions of the same name as PHP.

Some things don't translate so well (though those may make it into our experimental section though will probably excluded by default from the compiler), but it can be instructive to see how one might try it in JavaScript. And we had to take a few liberties where the languages did not have 1-1 equivalents (like treating objects as associative arrays as explained in the FAQ). But otherwise, we try to mirror PHP's functionality, arguments, return values, etc. as far as possible, so people can take what they want from PHP's function functionality while using JavaScript.

As far as explaining the benefits, I thought the main page and wiki/FAQ gave some idea about it. It is a wiki, so if anyone has ideas on that end, please go for it. But you're right, I think we could do better as far as marketing--we've focused mostly on the code or the site, despite being around for a while now...

Gravatar
Bruce Mills
Aug 3rd Permalink

q  I am a former product manager and have been design/coding in PHP, Javascript, ActionScript and JS frameworks for the past 10 years. I read about PHP.JS on Developer.com. It sounds intriguing. There are strengths and weaknesses to both PHP and Javascript (but they live in different spaces). I looked at the code. As best I can figure at a glance, the goal of PHP.js is to provide PHP functionality in a Javascript context. Am I getting this right? If not, I would like to know.

As a former marketer I think it would be nice if somewhere on this site someone wrote a simple positioning statement explaining the goal of this projects along with its features and benefits (like the jQuery site). A lot more people would 'get it' without having to tinker just to understand the objective--leading to greater support for it!

Gravatar
Kevin van Zonneveld
Apr 21st Permalink

q  Hey guys,

_phpjs_shared_bc should be added as function like any other, but due to a bug in the compiler that surfaced due to a different format of the _phpjs_shared_bc.js file, it isn't.

a fix is on it's way: http://github.com/kvz/phpjs/commit/a5a1da2881ea9df6351a4b35b3f5752ff869352e

Gravatar
Brett Zamir
Apr 21st Permalink

q  @Dan Bryant: Thanks for the report!
@Kevin: It looks like _phpjs_shared_bc is supposed to be added as a method of the namespaced object (according to how BC functions call it), but it is just being inserted as a function instead of a method:

...prototype = {
constructor: PHP_JS,
function _phpjs_shared_bc (...) {...}
};



Since this is not a public method, I think this might be better treated as a private static method, e.g., for inclusion immediately after var php_js_shared = {}; is defined. In that case though, the BC functions would need to be changed so that calls to _phpjs_shared_bc were not prefixed by "this.".

Gravatar
Dan Bryant
Apr 21st Permalink

q  Greets! Thanks for the awesome code library!

Just dropping you a note to let you know that your compile system is borked when including the BCMath Arbitrary Precision Mathematics functions (bc*). Firebug goes nuts and tries to kill me with my own mouse.

Its bad.

Example: http://phpjs.org/packages/download/2633/name:php.namespaced.min_00030.js

Anyway, when I remove these functions from the compilation everything is once again peachy ... I can once again frolic in the land of unicorns and pixies where nothing ever goes wrong while writing web apps...maybe not.

Example: http://phpjs.org/packages/download/2662/name:php.nobc.namespaced.min.js

Anyway. Thanks again for the awesome lib. Sorry for flipping out ;-)

-Dan

Gravatar
Brett Zamir
Apr 19th Permalink

q  @ams: Also, be aware that someone has reported a problem with our md5() function in case you are using that... Some of our earlier and even later functions really need better testing.

Gravatar
Brett Zamir
Apr 19th Permalink

q  @Ams: Thanks! Very cool to see the functions entering new environments. And your extension looks pretty useful. Be sure to keep up with the latest versions here.

FYI, I've used the functions in Mozilla extensions and am interested in developing a code module for them, since code modules are only loaded once across all windows: https://developer.mozilla.org/en/JavaScript_code_modules . No reason to reinvent the wheel, whatever your JS environment...

Gravatar
Ams
Apr 19th Permalink

q  Hello,
I am using phpjs in not ordinary way :) Besides other functions, I use phpjs library in my Google Chrome extension - JavaScript ToolBox.
Thanks for good work.

Gravatar
Brett Zamir
Jan 29th Permalink

q  @Paul Smith: To add to my last comment now, the change has been made to Git, but it may take a while to show up in the functions. Always check the "raw js source" on any function page for the latest.

Gravatar
Brett Zamir
Jan 29th Permalink

q  @Paul Smith: Thanks! Yes, I discovered that a while into my coding, but I guess I missed a few when I earlier tried to review this issue. FYI, although I was able to use "find-in-files" from Notepad++ to look for your example (it found the mode[i] in fopen and popen), if you see specific functions with problems, let me know the specific functions (I know, you probably thought the problem was systemic, but I think there really shouldn't be too many of these--I hope).

Gravatar
Paul Smith
Jan 28th Permalink

q  Hey, great code! But instead of accessing characters in a string via array index like

mode[i]

you should be using

mode.charAt(i)

.

The reason being because

mode[i]

chokes any version of IE older than 8. (I only care because I do commercial development and trying to get things to work with IE is the worst part of my job!)

Gravatar
Chris Loer
Jan 12th Permalink

q  @Kevin: Thanks, that language looks like it will resolve the lawyer concerns. We appreciate your flexibility!

Gravatar
Kevin van Zonneveld
22 Dec '09 Permalink

q  @ Chris Loer: I've updated the license page. Let me know if your lawyers run into any more problems with this. It is our intention that you can use php.js however you see fit. If they need some other text that follows this intention I'd be happy to place it.

Gravatar
Chris Loer
17 Dec '09 Permalink

q  I see your point about "or" also having ambiguity problems. I like Mozilla's wording. I think just about anything that clearly states "you can use either license" would make our lawyers happy (either within the license itself or in the "about the license" page). Although, of course, IANAL. If you or Kevin had a particular change in mind, I could run it by our legal department to see if it worked well for them: hopefully the benefit for the PHP .JS project would be that a change that satisfied our lawyers would also make the project more accessible to other people working within license-sensitive companies.

We haven't made any modifications so far, but if we do, we'd definitely like to be able to give them back. Our allergy to GPL is not so much the required opening of modifications as the "viral" part -- even though there are often "safe" ways for us to use GPL code, the cost of monitoring compliance from our end can be prohibitively high.

Gravatar
Brett Zamir
16 Dec '09 Permalink

q  That's Kevin's call, but I'd think that "or" is more ambiguous--as if we're not sure how we want to license it. :) Mozilla tri-licenses (see http://www.mozilla.org/MPL/ ), and I like their wording there, "Core Mozilla project source code is licensed under a disjunctive tri-license giving you the choice of one of the three following sets of free software/open source licensing terms:"

And although it is under MIT, esp. if you encounter any bugs, we'd still like improvements returned if possible! :) Also makes it easier for you to have your improvements already included if we update.

And there is also even a PHP-way for us to customize functions too, by using our "global" php_js.ini variable (e.g., if you set ini_set('phpjs.objectsAsArrays', 0), it will force is_array() to treat objects as not being arrays (since we usually consider them as such to support associative arrays)). So if you have some custom functionality you'd like us to integrate, it is also possible for us to do it in a "PHP way".

Gravatar
Chris Loer
15 Dec '09 Permalink

q  My company is planning to use parts of PHP .JS in an upcoming release of our product. We appreciate all the work the community has put in for us to take advantage of!

Our legal department, however, is concerned by a possible ambiguity in the license at (http://phpjs.org/compiled/license.txt): where it says that the code is "Dual licensed under the MIT and GPL licenses," they worry that this could imply that the code is constrained by *both* the MIT and GPL licenses. Although I think it's clear that the intent is to allow people to use whichever license works best for them, a formal clarification would make the lawyers much more comfortable. The simplest change would be to just change "MIT and GPL" to "MIT or GPL". jQuery goes a bit further in spelling out their intent (http://docs.jquery.com/License).

Would it be possible to make a similar change to the posted PHP .JS license?

Thank you!

Gravatar
Kevin van Zonneveld
14 Dec '09 Permalink

q  @ WebDevHobo: Implemented overflow:auto. Works for you?

Gravatar
WebDevHobo
13 Dec '09 Permalink

q  Hi there,

I just noticed this page: http://phpjs.org/functions/is_numeric:449

Where part of my function got integrated. Really gratefull, but if you go look at the page, you'll see the text partially disappear behind the column on the right.

I fear an

overflow:scroll;

is in order.

Using Firefox 3.5.5 on Windows 7.

Gravatar
Brett Zamir
30 Aug '09 Permalink

q  @Taras Bogach: Thanks much for alerting us to the issue. Rather than having to reimplement the third argument (and match your different naming of variables, etc.--btw, I recommend not using capital letters for regular variables as jslint suggests reserving them for constructors), I just added a call to krsort on 'from' in my own implementation which seems to have fixed the issue.

If you can better optimize what I have now in SVN (i.e., with the call to krsort), or provide a new and faster implementation with all arguments passing all test cases like mine is now, I can use yours instead. In any case, thanks for alerting us to the bug!

Gravatar
Taras Bogach
27 Aug '09 Permalink

q  Sorry, now I have faster code:

function(Input,Replace){
	var Output="",InputSize=Input.length,StringSize=0;
	for(Code in Replace)if(Code.length>StringSize)StringSize=Code.length;
	for(var Offset=0; Offset<InputSize;){
		var String="";DoReplace=false,ReplaceCode=null;
		for(var Size=StringSize; Size>0; Size--){
			String=Input.substring(Offset,Offset+Size);
			if(Replace.hasOwnProperty(String)===true){
				DoReplace=true;
				ReplaceCode=String;
				break;
			}
		}
		if(DoReplace==true){
			Output+=Replace[ReplaceCode];
			Offset+=ReplaceCode.length;
		}else{
			Output+=Input.substring(Offset,Offset+1);
			Offset+=1;
		}
	}
	return Output;
};

Gravatar
Taras Bogach
27 Aug '09 Permalink

q  Bug in strtr()
PHP:

echo strtr("aa",array("a"=>1,"aa"=>2));
//Will output: "2"


JS:


echo strtr("aa",array("a"=>1,"aa"=>2));
//Will output: "11"


The right JS code for function (You may add the 3-d parameter):


function(Input,Replace){
var Output="",InputSize=Input.length,StringSize=0;
for(Code in Replace)if(Code.length>StringSize)StringSize=Code.length;
for(var Offset=0; Offset var String="";DoReplace=false,ReplaceCode=null;
for(var Size=1; Size<=StringSize; Size++){
String=Input.substring(Offset,Offset+Size);
if(Replace.hasOwnProperty(String)===true){
DoReplace=true;
ReplaceCode=String;
}
}
if(DoReplace==true){
Output+=Replace[ReplaceCode];
Offset+=ReplaceCode.length;
}else{
Output+=Input.substring(Offset,Offset+1);
Offset+=1;
}
}
return Output;
}

Gravatar
Kevin van Zonneveld
4 Aug '09 Permalink

q  @ Miguel Sánchez Villafán: fair enough, added : )

Gravatar
Miguel Sánchez Villafán
24 Jul '09 Permalink

q  We use PHP.JS to enhance validation forms and encoding.

Gravatar
Kevin van Zonneveld
24 Jul '09 Permalink

q  @ Mark Goddard: added!

Gravatar
Mark Goddard
15 Jul '09 Permalink

q  I use PHP.js to simply decode html_entities that I grab through twitters API.

html_entity_decode(val.source)

Gravatar
Kevin van Zonneveld
14 Jul '09 Permalink

q  Cool, added!

Gravatar
Michael White
10 Jul '09 Permalink

q  Hey Kevin!

My new hosted content management system is making use of PHPJS.

The URL is http://getsprink.com

Gravatar
Kevin van Zonneveld
18 Jun '09 Permalink

q  @ Benjy: Ooh that was a really nasty bug, thanks a lot for bringing that to my attention. I fixed it up, tested it here:

http://phpjs.org/packages/view/917/name:e042ce5ee0716c3ba45da5519f70c744

And it seems to be working again. Happy compiling!

Gravatar
Benjy
16 Jun '09 Permalink

q  Hi,

Every time I try to get one of the namespaced packages, only the definition of the namespace is in the file, there are no functions (even if I try to make a custom package with only one function for example).
I think there's a bug with the namespacing... (occurs whether I choose minimify or not).

Thanks for your help,
Benjy

Gravatar
Brett Zamir
29 May '09 Permalink

q  @nLight, first of all, what exactly is so terrible about novices being able to quickly and easily make a webpage do what they want it to do?

If you're worried that there code will soon become part of standard libraries, there are probably a whole host of more serious problems a novice will have in upgrading their sites (such as relying on the more-terrible-than-PHP JavaScript global problem) than thoughtlessly relying too much on the very few functions in PHP.JS which are little more than adapter wrappers (and given the performance lengths to which people will go to do things like get separation of concerns, why not accept a little cost for cross-language familiarity?--one brilliant Assembly language, etc. programmer I know even complains about mixing up the languages sometimes).

But if you actually look at the functions and consider how the PHP functions (and our implementations) only build on the functionality in JavaScript (we are using JavaScript after all!), you should notice that most of the many PHP functions we use offer extra arguments which would be useful enough to be recognized (in any other library which was not named after PHP) as justifying an added performance cost even if that's all they did (while in fact most PHP.JS functions go far above and beyond any single function or method in JavaScript).

One could also argue that PHP.JS will actually help people learn the cooler features of JavaScript since the code is very accessible to study. There is nothing preventing one from using JavaScript fully and also using very convenient PHP functions. And what really is the difference between using a function to do some useful manipulation if the name reuses a PHP name, and one which invents yet another library-specific utility name? Doesn't this reuse serve DRY?

I also commented on a similar complaint at http://phpjs.org/pages/home#comment_50909

Gravatar
nLight
29 May '09 Permalink

q  Instead of learning such a great language as JavaScript is, monkey-coders will use this library to bring disgusting php functions in JS code.

Gravatar
silverfh
16 May '09 Permalink

q  cool work.. excellent and really helpfull.. I think this should be extended with jQuery or something like that..

keep it on , (2 thumbs up)

Gravatar
Kevin van Zonneveld
11 May '09 Permalink

q  @ urtechnology: when we change functions it will be visible in the functions rrs feed here:
http://phpjs.org/functions/index.rss

Gravatar
Gert
8 May '09 Permalink

q  Thanks for this great library..
Makes our life yet a little bit easier.

Gravatar

6 May '09 Permalink

q  i am very happy to use your code , is there any RSS Feed in your site ? , i like to update the latest update through the RSS Feed !

Any way it is a It's a great library!

Gravatar
Eugene Bulkin
5 May '09 Permalink

q  Alright, thank you. I'll remember to include it when I release :)

Gravatar
Brett Zamir
4 May '09 Permalink

q  Great to hear you like it, Paha...

Gravatar
Paha
3 May '09 Permalink

q  Guys, than you!
It's a great library!

Gravatar
Brett Zamir
30 Apr '09 Permalink

q  Yes, Eugene, I believe there should be no problem with including our library in a GPL3-licensed project, since our license is more permissive. You should include our license terms with your project, indicating to which portions of the code it refers (i.e., the PHP.JS code before any changes you make), and there should be no problem with your doing so with GPL. The problem occurs if you try to go the other way around (from GPL to a more permissive license).

Of course, if you make changes to the PHP.JS functions, it'd be great for you to release those parts back to us as well under our same license terms (i.e., not only under GPL3) so we can use such improvements in our project, but that's your call since our license doesn't prohibit you from restricting the modified source if you wish (including if you want to make your changes to PHP.JS subject to the GPL as well). best wishes, Brett

Gravatar
Eugene Bulkin
30 Apr '09 Permalink

q  Hi, I was just wondering... would a customized compile of php.js be allowed to be distributed in a GPL3-licensed project?

I would like to use it for my forum software: http://code.google.com/p/rambleforums/

Thank you :)

Gravatar
Kevin van Zonneveld
29 Apr '09 Permalink

q  @ Diogo Vargas: You got it!

Gravatar
Diogo Vargas
23 Apr '09 Permalink

q  We use your code! We wanna get linked!

Not exactly in our site, but in some sites we develop.

Great work, thanks!

Gravatar
Kevin van Zonneveld
19 Apr '09 Permalink

q  Excellent idea. I've put it directly under Development. But you can also review it at:
http://phpjs.org/compiled/license.txt

Gravatar
Brett Zamir
15 Apr '09 Permalink

q  Hello Brian...Good point--The licensing is described at http://kevin.vanzonneveld.net/techblog/article/phpjs_licensing/ . It's an MIT-style license (copy-center like BSD/Apache). Kevin, do you want to put that info maybe under "Develop"?

Gravatar
Brian Gant
14 Apr '09 Permalink

q  Hey all,

Just a quick question about PHP.js licensing... your website lists the software as open source. Is there a specific license that you are using (GPL2, LGPL2, GPL3, BSD, Apache, etc)?

Thanks in advance!

Sincerely,
Brian

Gravatar
Kevin van Zonneveld
14 Apr '09 Permalink

q  @ Jon Langevin: Thanks but I really like Cake, I just need to learn how to make it fast.. It's doable ;) Should be a lot faster now already.

Gravatar
Jon Langevin
3 Apr '09 Permalink

q  Kevin, you're correct, your blog doesn't suffer from the same performance issue :-)

Good luck with Cake! If you need a lighter framework, I suggest CodeIgniter, as it's never seemed as hefty as Cake.

Cheers

Gravatar
Kevin van Zonneveld
3 Apr '09 Permalink

q  @ Jon Langevin: I think the problem isn't my server. My blog is running on the same thing http://kevin.vanzonneveld.net and is servered swiftly (can you confirm that? maybe it's a transit issue)

I think I will have to blame CakePHP, and especially my little experience with it. I'm working on better caching so I hope things will be faster soon.

Thanks for your offer!

Gravatar
Jon Langevin
1 Apr '09 Permalink

q  Hey guys, nice project you have here. One issue I've noticed, is that your website is either under extreme load, or on a poor server, as it responds very slowly, the transfer rate is very low, etc.

Do you guys need some help with hosting or do you need a mirror? Or is the performance issue I noticed fairly uncommon?

Cheers

-Jon

Download

Download

There is a wide variety of packages if the default doesn't suit you.
You can also compile your own package to avoid any overhead.

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !

RSS

Tweets

Comments

Who uses php.js

If you use php.js, let us know and get linked.

Progress

php.js is complete for 82.2%

php.js on

Discuss php.js' future at Google Groups
Help improve php.js on github



Powered by php.js
Stats