0
     <?php $sizeh = '<script type="text/javascript">
                    document.write(viewportheight);</script>'; 

     echo $sizeh;
     echo  gettype ($num);

     $num = (int) $sizeh;
     //$num = intval($sizeh);
      echo  gettype ($num);
      echo $num;

 ?>

This results is:

1083  string  integer   0

Why I am getting the value of zero while that zero should be 1083 ??

3
  • 2
    PHP is executed before JS. So you are casting the entire js string not its result. Commented Aug 19, 2011 at 14:57
  • 1
    How is $sizeh 1083? Your PHP script cannot see the values of javascript like that. Commented Aug 19, 2011 at 15:00
  • Please learn about the basics of the client-server model and consequently the distinction between server-side executed code (PHP on the webserver) and client-side executed code (Javascript in the user's browser). It's essential knowledge for a web developer. Commented Aug 19, 2011 at 15:05

4 Answers 4

2

you're trying to change int value from $sizeh, not from $num. $sizeh is that your javascript code

Look here

    <?php 
   $sizeh = '<script type="text/javascript">
                    document.write(viewportheight);</script>'; 
     $num = "1083";
     echo $sizeh;
     echo  gettype ($num)."  ";
     echo $num."  ";
     $num = (int) $num;
     //$num = intval($sizeh);
      echo  gettype ($num)." ";
      echo $num;

 ?>
Sign up to request clarification or add additional context in comments.

Comments

0

You are mixing outputs from both JavaScript and PHP. 1083 is being written by JavaScript code. string, integer and 0 are being echoed by your PHP code.

Comments

0
 <?php $sizeh = '<script type="text/javascript">
                document.write(viewportheight);</script>';

$sizeh now is this ugly string. You will see 1083 in your browser because your browser run JS. But it will after php stop works

Comments

0

The problem is that you are looking at the browser window and not at the source code: $sizeh is a string, starting with <script type .... When you cast that string to an integer, the result is 0.

You cannot capture the output of javascript in php as the php is processed first, then sent to the browser after which the browser executes the javascript. To send a javascript value back to php you would need to post a form or use ajax.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.