1

for my web application i need to use an integer value of variable of javascript in php so that i can extract data from database in mysql.so as a test i tried this:

<script>var p1=7123;</script>
<?php
    $a= "<script>document.write(p1);</script>";
    echo $a;
    $b=$a;
    $b=intval($a);
    if(is_int($b)){echo "numeric ";}
    echo "$b ";

?>

i thought $a would be numeric string. so i converted it to integer and stored it in $b but then $b has the value zero.But the following works so i guess the problem is with document.write().Is it the problem?

 <?php
    $a= "7123";
    echo $a;
    $b=$a;
    $b=intval($a);
    if(is_int($b)){echo "numeric ";}
    echo "$b ";

?>
Aanchal Adhikari
  • 303
  • 4
  • 12

3 Answers3

1

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.

musicin3d
  • 1,028
  • 1
  • 12
  • 22
0

The PHP code runs on the server, and at that time whatever you echo to the output buffer is only treated as text. It's not until the page is loaded in the browser that the JavaScript code is executed.

So, you can't get a value from JavaScript into a PHP variable, they run at different times in different places. The PHP code runs on the server, and when it completes the page is sent to the browser where the JavaScript runs. When the JavaScript starts, the PHP code has already finished.

The variable $a contains the string "<script>document.write(p1);</script>", and writing it out won't execute the script in it on the server. The variable still contains the script when you try to convert it to an integer.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • so then isn't there any other way to do it? – Aanchal Adhikari Aug 02 '15 at 15:10
  • @AanchalAdhikari: You can't do it in a single page load. You would have to send the value back from the browser to the server in another request. You can use an AJAX request if you don't want to make a postback. – Guffa Aug 02 '15 at 15:11
0

As mentioned in THIS ANSWER "PHP will coerce the type for you in most circumstances", but if you want to be explicit try:

<?php
    $a = "7123";
    $aNum = (int)$a;
?>
Community
  • 1
  • 1
Brian Peacock
  • 1,801
  • 16
  • 24