Home Tutorial Php PHP Variables in JavaScript

 
 

PHP Variables in JavaScript
Posted on: January 14, 2010 at 12:00 AM
Advertisement
JavaScript and PHP both are scripting language for the web. Moreover, JavaScript is basically a client side scripting language and PHP is server side scripting language.

PHP Variables in JavaScript

JavaScript and PHP both are scripting language for the web. Moreover, JavaScript is basically  a client side scripting language and PHP is server side scripting language.

Both are used to add functionality, validate forms and  so on. JavaScript is used to give additional effect on the client side. JavaScript is the most popular scripting language on the internet.

Let's see how can you use PHP variables in JavaScript. For example of PHP Variable in JavaScript:

<title>PHP Variable in JavaScript</title>

<?php

$php_var = 1;

?>

<script type="text/javascript">

num = <?php echo $php_var; ?>;

document.write(num);

</script>

The output of the above example is : 1.

 In the above example. In the first part, we declared a variable in PHP code as :

<?php

$php_var = 1;

?>

 In the second part, we declared a variable in javascript called num and then initialized as :

num = <?php echo $php_var; ?>;

In the next line, we have given the command to print by writing :

 document.write(num);

So, in this way you can use PHP variables in JavaScript.

Advertisement

Related Tags for PHP Variables in JavaScript:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 14, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com