PHP如何与HTML交互,反之亦然? [英] How does PHP interact with HTML and vice versa?

查看:115
本文介绍了PHP如何与HTML交互,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习互联网和网站的工作方式。我想我理解PHP处理器如何处理.php文件:



浏览器请求以.php结尾的网页,并向该网页的服务器发送请求。

服务器询问'我可以找到那个页面吗?'

如果服务器找不到它,服务器发回错误。

如果服务器找到页面,服务器询问'是否有文件扩展名以.html或.php结尾'?

如果以.html结尾,服务器将页面发送回浏览器。

如果以.php结尾,请将页面转至PHP处理器。

PHP处理器逐行扫描页面。它构建一个处理过的HTML页面。

如果发现HTML,它会将它作为正在构建的已处理HTML页面的一部分传递。

如果发现PHP脚本,则PHP处理器可能会或可能不会输出HTML。

PHP处理器完成后,处理后的页面将传递到Web服务器以发送给浏览器。



HTML是否仍然为.php文件提供结构,HTML的结构与使用CSS的网页的结构相同?当我说结构时,我的意思是分裂,头部和身体部分。结构与网页的裸露骨骼骨架一样。看起来,在一些网页上,PHP的数量比HTML多。有时候只有最低限度的HTML,剩下的就是PHP和Javascript,这是网页制作的方式吗?



总之,一个拥有PHP代码的网页在它需要有一个.php扩展名。一旦PHP处理器遇到<? PHP <?,服务器将代码发送到PHP处理器(这种措辞可能是多余的)。 PHP处理器的输出可以是任何东西。

任何包括:


  • Css

  • HTML


  • XML

  • XHTML?

  • 图片

  • 声音?

  • 视频?

  • 动画,如flash?



显然强制内容类型可能需要,但可以完成。

HTML是所见即所得。

要在网页到达用户浏览器后控制和操作网页,可以使用Javascript。随着移动设备和几种不同类型的浏览器的出现,jQuery的发明使得开发JavaScript程序变得更加容易。 解决方案

HTML是网络的语言。这是一种标记语言,这意味着我们唯一可以使用它的是标记文档,即设计内容将如何呈现给最终用户。

想象一下,我们有一个向用户显示日期的页面。



我们可以使用一些HTML来做到这一点:

 < p> 2012年8月26日星期日< / p> 

但是,我们希望保持该页面是最新的。我们必须每天手动更改日期。由于HTML是静态的,因此不能动态更改。

也许根据页面何时被加载,能够自动生成正确的日期到页面会很有用。

这就是PHP的用武之地.PHP是一种脚本语言,虽然它可以用于许多事情,但其主要用途之一是动态生成HTML。因此,不要写今天的日期 - 我们可以做的就是使用一些PHP并说。

 < p>< ;?回声日期(l j F Y);?>< / p> 

这将打印出2012年8月26日星期日今天2012年8月27日星期一 , 等等。



我需要将这个新版本的页面保存为page.php而不是page.html,因为我需要我的服务器(它使用PHP )将页面发送给PHP解释器。它会寻找特殊的<?php <?并尝试处理它发现的东西。在这种情况下,它会为我的页面上的日期分配正确的文本,并在将其发送给用户之前将其添加到页面中。

我们可以用PHP做很多很酷的事情。它是服务器端技术,意味着它在服务器上完成它的工作,然后向我们发送添加了所有动态内容的完成页面。

有时我们可能想要在页面到达用户浏览器后控制和修改页面。为此,我们需要一些客户端技术,即在用户浏览器中运行的代码。最常用的客户端语言是javascript。

我们也可以用Javascript做很多事情,但大多数情况下我们会在网页中使用它来允许我们在HTML页面到达用户后控制元素。

我们可能希望在网页上隐藏某些内容,然后仅在用户点击某个按钮后才显示该内容。我们可以用javascript来做到这一点。



现在因为Javascript是客户端技术,即它在您的浏览器中运行,实际上可能很难使用,因为您会必须编写适用于各种不同浏览器的代码,现在也可以在手机上使用!为了使这项工作更容易,非常聪明的开发人员通过创建库和框架来使用JavaScript来控制网页中的元素,从而消除了很多痛苦。其中最受欢迎的是 jQuery框架。我认为jQuery是最有趣的东西,因为它可以让你在网页中做所有的酷东西 - 使东西淡入淡出,淡化东西,播放声音,移动元素等等。

b
$ b


我希望这可以帮助您计算出不同的技术如何帮助您实现不同的目标。

TL; DR 版本可能是:

HTML& CSS - 介绍你的页面的外观。



PHP - 帮助你动态生成HTML。



JavaScript - 可帮助您使页面更具互动性,并可响应用户的点击或其他操作。

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 sends request to server for that webpage.
Server asks 'can I find that page?'
If server does not find it, server sends back error.
If server finds page, server asks 'does file extension end in .html or .php'?
If it ends in .html, server send page back to browser.
If it ends in .php, hand page to PHP processor.
PHP processor scans the page, line by line. It build a processed HTML page.
If it finds HTML, it passes that on as part of the processed HTML page it is building.
If it finds PHP scripts, the PHP processor may or may not output HTML.
When the PHP processor is done, the processed page is handed to the web server for sending to the browser.

Does HTML still provide structure to a .php file, the same way HTML provides structure to a webpage that also uses CSS? When I say structure I mean divisions, and head and body sections. Structure as in the bare bones skeleton of the webpage. It seems that on some web pages, there's more PHP than there is HTML. Sometimes there's the bare minimum HTML, and the rest is PHP and Javascript, is this the way webpages were made to be?

In summary, a web page that has PHP code in it needs be have a .php extension. Once the PHP processor encounters <? PHP or <?, the server sends the code to the PHP processor (this wording is probably redundant). The output of the PHP processor can be anything.
Anything includes:

  • Css
  • HTML
  • Javascript?
  • XML?
  • XHTML?
  • Images
  • Sound?
  • Video?
  • Animations such as flash?

Apparently forcing content type may be needed, but it can be done.
HTML is WYSIWYG.
To control and manipulate a web page after it has reached the user's browser, Javascript can be used. With the advent of mobile devices and a few different types of browsers, jQuery was invented to make the developing Javascript programs easier.

解决方案

HTML is the language of the web. It is a markup language which means that the only thing we can use it for is to "markup" documents, i.e. design how content will look to the end user.

Imagine we had a page that showed the user the date.

We could use some HTML to do that:

<p>Sunday 26 August 2012</p>

But say we wanted to keep that page up to date. We'd have to go and manual change the date manually everyday. Because HTML is static, it can't be changed dynamically.

Perhaps it would be useful to be able to generate automatically adding the correct date to the page, depending on when the page is loaded.

That is where PHP comes in. PHP is a scripting language, and while it can be used for lots of things, one of its main uses is to generate HTML dynamically. So instead of writing in today's date - what we could do is use some PHP and say.

<p><? echo date("l j F Y");?></p>

This will print out for me "Sunday 26 August 2012" today, "Monday 27 August 2012" tomorrow, and so on.

I'd need to save this new version of my page as page.php instead of page.html, because I need my server (which is set up use the PHP) to send the page to PHP interpreter. It will look for the special <?php or <? and try to process what ever it finds. In this case it spits out the correct text for the date on my page and adds it to the page before sending it to the user.

We can do lots of cool stuff with PHP. It is "server side" technology, meaning that it does its work on the server and then sends us the finished page with all the dynamic content added.

Sometimes we might want to control and modify a page after it has reached the user's browser. For this we will need some "client side" technology, i.e. code that runs in the user's browser. And the most common client side language of choice is javascript.

Again we can do a lot with Javascript, but most often we use it in web pages to allow us to control elements of a HTML page after it has reached the user.

We might want to hide something on a page and then only show it once a user has clicked a button. We can do that with javascript.

Now because Javascript is "client side" technology, i.e. it runs in your browser it can actually be quite hard to use, because you will have to write code that works in a variety of different browsers, and now on mobile phones too! To make this job easier, very smart developers have taken a lot of the pain out of using javascript to control elements in web pages by creating libraries and frameworks to use. One of the most popular of these is the jQuery framework. I think jQuery is the most fun thing to learn, because it allows you to do all of the "cool stuff" in webpages - make stuff fade in, make stuff fade out, play sounds, move elements around etc etc


I hope this helps you work out how different technologies can help you achieve different things.

The TL;DR version of this would be:

HTML & CSS - sets out how your pages are going to look.

PHP - helps you to generate HTML dynamically.

JavaScript - helps you make your pages more interactive and can respond to clicks or other actions of your user.

这篇关于PHP如何与HTML交互,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆