Page 1 of 1

Are php and java script identical

Posted: Tue Sep 01, 2009 11:31 pm
by droose
Hi All,
I am a 73 year young retired engineer and I stay young by programming and designing web pages. I am in the process of teaching myself php and I have noticed that php is very simular, if not identical to java. Even to the built in functions like "document.location.replace". Am I correct?

Can I change java scripts to php scripts so the code runs on the server rather than in my local machine. I would like to do this so that the user would not need to enable java scripts each time the script is run. I find this annoying and I believe a lot of people would rather not need to stop and enable java scripts.

Thanks
droose

Re: Are php and java script identical

Posted: Tue Sep 01, 2009 11:47 pm
by requinix
Uh...

Apples and oranges. They are not identical. Not even close.

Re: Are php and java script identical

Posted: Wed Sep 02, 2009 12:22 am
by droose
Thanks for the answer tasairis,
I am still not totally convinced. Yes, there are differences in the syntax but the basic syntax and program flow is the same. If I convert my java script to php (i.e. renaming variables and changing some built in functions like .string to substr) and upload it to the server, will it perform the same functions as it does in the java version. From what I have seen the conversion should be pretty straight forward.

Re: Are php and java script identical

Posted: Wed Sep 02, 2009 3:05 am
by Mark Baker
First off, don't confuse java with javascript.

Biggest single difference between javascript and PHP is that javascript is typically event driven (e.g onMouseOver) and executed within the constraints of a browser (yes, I know there is a server version, but I've yet to identify a use for it), whereas your PHP script is only executed when explicitly requested (e.g. by a server request, or by typing in a request to run on the command line) and is typically run as an apache module.

Javascript has no file handling functions for very obvious reasons.
PHP has no browser event handling functions.

javascript is more heavily OOP oriented, whereas PHP is predominantly function oriented. e.g.

Code: Select all

var d = new Date();
var curr_date = d.getDate();
or

Code: Select all

$curr_date = date('j');
Javascript you instantiate a date object, then call a method to retrieve the day of month: PHP we call a date() function, passing parameters to identify what we want returned.

Actuallynot a good example, because PHP does now have a date object. But the principle is the same for most data types. Strings are objects in js, but not in PHP

There are some similarities in the control statements of javascript and PHP... while loops, if statements, etc.... but you'll find the same for the majority of programming languages; and familiarity with one language is typically useful when learning another... but that applies when thinking in terms of the principles of program structure and design... the actual syntax can, and typically does, vary considerably.