Strange, This PHP seems to be correct, but it cannot get the correct result.

Source: Internet
Author: User
Strange, This PHP seems to be correct, but it cannot get the correct result.
$ Screen_w = "script document. write (screen. width); script "; if ($ screen_w <500) {echo 'width less than 500 ';} else {echo' greater than or equal to 500 ';}


Directly echo $ screen_w; the correct result is 1366.

If the result is 1366 or less than 500.

If (int) $ screen_w or intval ($ screen_w)

It is displayed as 0.

Why?


Reply to discussion (solution)

Is it true if there is no syntax error?

1. php is executed before js! Even if $ screen_w = "script" document. write (screen. width); script "; can get data, it is after the php code is executed.
2. $ screen_w = "script document. write (screen. width); script";
What is it? It is only a string value assignment.
Even with echo $ screen_w;
It is only displayed in the browser.

Var_dump is a 46-bit string, and the JS code is included. match the number with the regular expression and then compare it.

I don't know how to get the numbers.

I used to have this problem, but the foundation is not strong.

Js and php should be separated,

First, you must understand what is the client code and what is the server code. Second, the server code is executed first, and the client code is executed later.

However, php cannot get the screen width. I want to write a sentence to judge that when the screen width is smaller than the width of the main content, no floating bar will be output that will block the content.

These questions are almost the only way for new users. for more than a decade (even in the past, haha), why don't I write this in all kinds of teaching materials?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.