run javascript in php code example

Example 1: call javascript function from php

// The most basic method
<?php
echo '<script type="text/javascript">',
     'someJsFunc();', // Or Whatever
     '</script>';
?>
  
// However, if what you are trying to achieve requires more complexity,
// You might be better off adding the V8JS module, see link below

Example 2: php execute javascript

<?php
 if(your condition){
     echo "<script> window.onload = function() {
     yourJavascriptFunction(param1, param2);
 }; </script>";
?>

Example 3: how to call js function from php

<?php
 if(your condition){
     echo "<script> window.onload = function() {
     yourJavascriptFunction(param1, param2);
 }; </script>";
?>

Example 4: how to run php inside js

You can't run PHP with javascript. JavaScript is a client 
side technology (runs in the users browser) and PHP is a 
server side technology (run on the server). If you want to 
do this you have to make an ajax request to a PHP script and 
have that return the results you are looking for.

Tags:

Php Example