This is very important...
PHP is executed on the server. The server then sends everything outside of the tags to the server along with any echo and print results. Then, the browser interprets everything the server sent to it. That's when JavaScript is executed.
Let's walk through your code...
<script>var p1=7123;</script>
The server spits that out to the browser immediately. No processing.
<?php
That engages the PHP engine. From here on, code will be processed as PHP.
$a= "<script>document.write(p1);</script>";
Now, in PHP, the $a
variable contains a string. It literally holds "<script>document.write(p1);</script>".
echo $a;
That prints (or "echos") the value of $a to the browser. PHP doesn't care what it is. It just sends it on it's merry way.
$b=$a;
$b
is now a copy of `$a'. It has the value "<script>document.write(p1);</script>", which is just a string.
$b=intval($a);
Now, we've thrown out the old value of $b
and replaced it with the integer value of $a
. According to PHP's documentation, any string which isn't a number will result in 0 (zero).
if(is_int($b)){echo "numeric ";}
That should really be on two or three lines, but we'll go with it. $b
is absolutely a number since we just assigned it to 0. So, we echo "numeric " to the browser.
echo "$b ";
Simple. We just echo $b
, which as we know is 0.
?>
That disengages the PHP engine. PHP will not be processed past this point. Anything that comes after this will simple be sent straight to the browser as if it were in an echo statement.
Let's look at what the browser got (with some line breaks added to make it more readable)...
<script>var p1=7123;</script>
<script>document.write(p1);</script>
numeric
0
The browser takes that, executes anything inside the tags, and displays anything else. Notice that this is very similar to what PHP did.
So what you will see in your browser is similar to...
7123
numeric
0
Again, I added line breaks so that you can see how everything matches up. In reality you would see something like 7123numeric 0
I hope that helps. It's critical to understand the flow of your code. Server code is executed only on the server. Client (browser) code is only executed on the client. When you are writing PHP, you are essentially writing server code that writes client code.