0

I have a file written in PHP. I would like to have inline if/else statements of JavaScript in PHP. Is it possible? An example is given below:

    <?php

     echo "<script type=\"text/javascript\">
             function myFunction(a, b) {
                      if(a>b)
                          return true;
                      else
                          return false;
                       }

              if(myFunction(3,2)) { "; 
                     $x = '/index';
              echo "} else { ";
                           $x = '/index2';
                     echo "}";
     echo "</script>";

     ?>
programmer
  • 4,571
  • 13
  • 49
  • 59
  • 2
    Yes it is possible. Where is the problem? – chandresh_cool May 26 '15 at 09:48
  • What do you want to do? – someOne May 26 '15 at 09:50
  • 1
    You are trying to assign value to php variable $x after javascript condition.. this is not possible as javascript executes after php has finished – abh May 26 '15 at 09:51
  • 5
    Remember the order of things. PHP runs on the server, it creates the page and sends it to the browser. Then the browser runs the Javascript. PHP isn't running any more, you can't assign to PHP variables from Javascript. – Barmar May 26 '15 at 09:51

1 Answers1

0

Well the answer is No!. You can't use it in PHP as interpretation engine will treat it as JavaScript syntax until the </script> tag ends.

Daan
  • 12,099
  • 6
  • 34
  • 51
Yogesh Pawar
  • 336
  • 2
  • 17