Are php and java script identical

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
droose
Forum Newbie
Posts: 2
Joined: Tue Sep 01, 2009 11:21 pm

Are php and java script identical

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Are php and java script identical

Post by requinix »

Uh...

Apples and oranges. They are not identical. Not even close.
User avatar
droose
Forum Newbie
Posts: 2
Joined: Tue Sep 01, 2009 11:21 pm

Re: Are php and java script identical

Post 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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Are php and java script identical

Post 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.
Post Reply