1

以下は、画面幅を変数に割り当てているコードです$width。しかし、整数に変換してエコーすると、0返されます。

$width = '<script> document.write(window.screen.width); </script>';
//if i echo $width it shows 1366 which is width of my screen
$height = '<script> document.write(window.screen.height); </script>';
echo "The height is = $height"."<br>The width is = ".$width;
$a = (int)($width);
echo $a;

この変換を完璧にするためのガイダンスを求めています。

4

4 に答える 4

5

その時点では、実際には画面の寸法を取得していません。それはクライアント上で実行されます。設定するには、サーバーに ajax 呼び出しを行う必要があります。

于 2012-08-09T12:57:51.887 に答える
1

JavaScript を使用して PHP 変数をエコーアウトすることはできません。JavaScript は、PHP が長い間処理を完了するまで実行されません。JavaScript でできる最善の方法は、データを ajax で戻すことですが、これは新しい PHP スクリプトでのみ使用でき、現在のページでは使用できません。

スクリプトの実行が完了する前にデータが返されることは期待できません。

于 2012-08-09T12:57:58.257 に答える
1

It would be intval but what you are doing isn't possible because you cannot pass client data from the browser through to a php script without $_POST, $_GET, or the similar.

于 2012-08-09T12:59:44.337 に答える
1

You don't need parantheses. Try $a = (int) $width; or $a = intval($width);.

Also, there is no sense in the code you are running.

$width = '<script> document.write(window.screen.width); </script>';

This will just set $width to be the string containing the HTML code (with the JavaScript inside). You can't fetch the return value of the JavaScript code that easily into a PHP variable, mainly because the JS part is executed way after the PHP is done (PHP is handled by the server, the generated HTML (with JS inside) is sent to the browser and the browser takes care of executing JS). To do that, you will need AJAX.

于 2012-08-09T12:59:53.620 に答える