Parsing Javascript Using PHP

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
bradbury
Forum Commoner
Posts: 40
Joined: Wed Aug 25, 2010 11:21 am
Location: Eugene, OR

Parsing Javascript Using PHP

Post by bradbury »

I am currently working on a calendar application that I made using javascript but need to get some values from a MYSQL database so I used PHP to do all that business. So my question was since PHP is a server side scripting language and javascript is a client side scripting language, when PHP parses the Javascript is the code read through my server instead of using the client to do some of the processing?
I could be completely wrong about all of this I was just curious and the only way to learn is to ask questions
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Parsing Javascript Using PHP

Post by AbraCadaver »

If your page is PHP then it will be parsed for PHP and the result sent to the browser where the JS will be interpreted. So given this:

Code: Select all

<?php

$message = 'Welcome!';

?>

<script type="text/javascript">
   alert('<?php echo $message; ?>');
</script>
Then this will be sent to the browser:
[text]<script type="text/javascript">
alert('Welcome!');
</script>[/text]

HTH
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
bradbury
Forum Commoner
Posts: 40
Joined: Wed Aug 25, 2010 11:21 am
Location: Eugene, OR

Re: Parsing Javascript Using PHP

Post by bradbury »

But if I am calling the 'javascript' like this:

Code: Select all

<script language="JavaScript" src="js/calendar.php"></script>
and all the javascript lines are called using:

Code: Select all

echo "function(){
//whatever code I have written
";
$con(mysql_connect(.....);
// more php then back to javascript
echo "function(){
.......
With this is the php module doing all the work on my server or is the client getting some of the work load?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Parsing Javascript Using PHP

Post by AbraCadaver »

Anything in PHP tags will be parsed and executed by PHP (db queries, echos etc.) and the result passed to the browser. At that point whatever pure javascript code is there will be executed in the browser.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
bradbury
Forum Commoner
Posts: 40
Joined: Wed Aug 25, 2010 11:21 am
Location: Eugene, OR

Re: Parsing Javascript Using PHP

Post by bradbury »

Oh kk. Well that makes sense then. I was just thinking about how if the php parsed the javascript and actually executed it then sent it to the browser how that might be a bit of an issue. But if php parses it and then sends it to the browser once it is parsed then there shouldnt be any major issues
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Parsing Javascript Using PHP

Post by pickle »

PHP doesn't parse Javascript or any language other than PHP.

When you have a file with a .php extension, the PHP interpreter looks through it for "<?php" (or "<?" if shorttags are on). Anything between <?php and ?> is treated as PHP code. Period. If Javascript is in there, it had better be in a string, or the PHP interpreter is not going to know what to do with it.

As far as the PHP interpreter is concerned, in any given file there is PHP code and ... everything else. Only PHP code is parsed. Everything else is just passed through as-is.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply