How does PHP interact with HTML and vice versa?

后端 未结 4 1772
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 01:23

I\'m learning how the internet and websites work. I think I understand how .php files get processed by the PHP processor:

Browser requests webpage ending in .php and

4条回答
  •  抹茶落季
    2020-12-11 01:54

    There's nothing really special about PHP. The basic difference is between static files and dynamic files written in a programming language.

    Static files are simply sent directly by the server to the browser. These aren't just HTML, this is also done for image files. And when you download applications or PDF, the same mechanism is used -- it might be a ZIP file, an EXE, a disk image (common for Mac downloads).

    In the case of dynamic files, the file is executed in some way, and the output it produces is sent to the browser. The dynamic file can be in any language -- it could even be a binary compiled executable. However, scripting languages have generally been most popular, simply because they tend to be easier to write web applications in. And as a result, there are lots of libraries that have been written to support web applications -- it's a positive feedback situation. In the early days of the web, Perl was probably the most common language; we didn't have the plethora of scripting languages that we do now.

    What makes PHP somewhat special is that it was designed specifically for scripting web pages. In all other languages, you have to write explicit commands to produce any output. The PHP processor simply outputs the file contents verbatim until it encounters the marker (there are also some other markers that it recognizes). Then it starts executing the program until it sees ?>, at which point it reverts to verbatim output.

    Another way to think of it is that anything outside is treated as if it were a big echo statement. In fact, this model is necessary to understand that you can actually switch modes in the middle of a statement. You can do:

    
        some text
        
        some other text
        

    This is obviously a silly way to output just one line, but imagine if it were a huge block of text. Basically, PHP's design allows you to write a normal HTML web page, and then embed programming code where you need it to produce dynamic content.

    And while it's most common for PHP scripts to output HTML, they don't always. It's not uncommon to have a PHP script that outputs images. It might do it by using a database to store the image or the location of an image file. It also has built-in and library functions that can generate image data on the fly. For instance, if you go to a web site that produces graphs, those graphs could have been produced by a PHP script.

提交回复
热议问题