仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1457|回复: 19
打印 上一主题 下一主题

[学习教程] PHP网站制作之PHP 简介

[复制链接]
小女巫 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:21:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
从刚开始练习的PHP基础语法练习,到PHP语言在WEB中的应用,再到实际的项目开发,如留言版,相册系统,中小型公司网站系统,以及期间做过的有关团队合作的小游戏,让我受益匪浅,学到了很多。   Joe Brockmeier 扼要引见了 PHP 剧本言语,会商了 PHP 的发源、功能和合用的平台。一个复杂的 PHP 剧本示例则侧重申明了其根基语法和用法。



假如您从事基于 Web 的开辟任务,那末能够已传闻过 PHP。您或许不太切实地晓得 PHP 是甚么、若何任务或为何如斯抢手,但如今该是进一步懂得 PHP 的时分了。因而本文扼要引见了关于 PHP 基本的根基概念。

一点后台常识
PHP 是作为一个小开放源码,跟着愈来愈多的人意想到它的适用性从而逐步开展起来。Rasmus Lerdorf 在 1994 年宣布了 PHP 的第一个版本。从那时起它就飞速开展,并在原始刊行版上经由有数的改善和完美如今已开展到版本 4.0.3 。

PHP 是一种嵌入在 HTML 并由办事器注释的剧本言语。它可以用于办理静态内容、撑持数据库、处置会话跟踪,乃至构建全部电子商务站点。它撑持很多盛行的数据库,包含 MySQL、PostgreSQL、Oracle、Sybase、Informix 和 Microsoft SQL Server。

静态与静态内容
静态内容为何这么抢手?假定您正在办理有 10 个产物的电子商务站点。只需产物不是常常变化或意料到它不会有太大的变化,那末手工编写 10 个带有需要的信息、表单和诸如斯类内容的静态产物页面是不坚苦的。然而,假定您在本月再要添加 10 个或更多产物,然后鄙人个月要更多,并且价钱有时会变化或想改动站点的不雅感。那末您就会堕入用手工从头编写数十个,或许上百个静态页面的窘境中。

另外一方面,假定您从创立 product.php 页面入手下手。它没有静态信息,而是编码成可以从产物数据库中提守信息并静态地构建一个页面。然后您就具有了一个元数据页面,它可以依据存储在数据库中的信息供应一个、一百个、乃至十万个独自页面。如今网站办理员不再成天都复杂反复更新静态页面的任务,由于在更新公司数据库中的信息同时就能够更新页面上的信息。如许就消弭了使人头疼的工夫延迟(在数据库中更改信息和在网站上显示信息之间的工夫距离)。

整体来讲,PHP 十分合适 Web 上的任务。但它并非独一的办法;如 Perl、Java、JavaScript、ASP、Python、Tcl、CGI 和其它很多办法都可以生成静态的内容。然而,PHP 的长处是:它是专为基于 Web 的成绩而设计的和它是开放源码。

假如您正在为文字处置或 3D 游戏寻觅法式设计言语,那 PHP 能够不是您所需求的言语。假如您需求运转一个具有静态内容、数据库交互和电子商业的网站,那末就请持续读下去,由于 PHP 在这方面的确长短常有效的。

PHP 合用的平台
大多半惯例 PHP 的装置凡是是与 Linux 或各类 UNIX 上的 Apache 一同运转的 PHP 模块。然而假如正在利用其它平台,不要忧虑。PHP 可以在 Windows NT 和 9x 和其它很多 Web 办事器上运转。可以在次要引见 Apache/Linux/PHP 组合的一些网站上找到更多有关 PHP 的文档,但它并非撑持 PHP 的独一平台。

允许证和利用
购置具有全体功效的嵌入式 Web 剧本言语要花几何钱?一分钱不花?PHP 是一个开放源码项目,所以没有购置允许证的费用或限制利用的成绩。您可使用 PHP 来运转小的、非获利性站点,或运转十亿美元的电子商务网站,并且本钱是一样的:零。不但如斯,假如想要或需求修正 PHP,可以修正它。

PHP 并没有失掉 GPL 的允许,但它本人的允许证答应从头分发代码和/或二进制文件。

利用 PHP
好,如今您已确信要真正地测验考试一下 PHP 了吧。咱们先看一些复杂的例子,如许您对 PHP 就有个也许懂得。记住决这不是深切懂得 PHP 的路子,仅仅是个疾速入门罢了。

"Hello, World!"
为了对 PHP 有个懂得,让咱们来看一下几个十分复杂的 PHP 剧本。既然 "Hello, World!" 是个经常使用的示例,那咱们就编写一个友爱的小 "Hello, World!" 剧本。

如早些时分所提到的,PHP 是嵌入在 HTML 中的。(能够您的文件几近没有包括 HTML,然而凡是这个文件是 PHP 与 HTML 的夹杂体。)这意味着在您正常的 HTML 中(或 XHTML,假如您处在对照前沿的地位),会有相似如许的 PHP 语句:

<body bgcolor="white">
<strong>How to say "Hello, World!"</strong>
<?php echo "Hello, World!";?>
<br>
Simple, huh?
</body>





很复杂,不是吗?这仅仅是一个 "echo" 语句,就如许。固然,仅仅如许是没有多大用途的。然而它的确告知咱们关于言语的一些器材。(特地说一下,假如反省 HTML 输入,就会注重到 PHP 的代码并没有呈现在从办事器送到您 Web 阅读器的文件中。一切呈现在 Web 页面中的 PHP 城市被处置并从页面中剥离;从 WEB 办事器前往给客户机的仅仅是纯 HTML 输入。)

在 Web 页面上打印日期和工夫
如今咱们做一些略微适用的工作。这个示例将在 Web 页面上打印日期和工夫。

<body bgcolor="white">
<strong>An Example of PHP in Action</strong>

<?php echo "The Current Date and Time is:<br>";
echo date("g:i A l, F j Y.");?>
// g = the hour, in 12-hour format
// i = minutes
// A = print AM or PM, depending...
// l = print the day of the week
// F = print the month
// j = print the day of the month
// Y = print the year - all four digits






此代码生成以下输入:


The Current Date and Time is:
11:00 AM Friday, October 20 2000.

请注重,这里揉和了 PHP 和 HTML。假定您已懂得 HTML,所以这里仅注释 PHP 代码。在 PHP.net (请参阅参考材料)上可以找到完全的 PHP 参考。

PHP 代码是以标志 <?php 入手下手并以 ?> 停止的。这就告知办事器在 <?php 和 ?> 之间的一切内容需求用 PHP 指令停止语法剖析,假如发明它们,就需求履行它们。请注重,当处置和办事您的文件时,客户时机收到通俗的 HTML 文件。阅读您站点的人看不就任何一个的 PHP 指令,除非您犯了毛病,办事器把这些 PHP 代码朋分开而没有先处置它们。

普通情形下会处置在 <?php 和 ?> 之间的惯例 HTML 标志。请注重下面这个复杂的剧本中包括了 <br> 这个分行标志。假如不克不及够很好天时用 HTML 格局,那末 PHP 就不会十分有效。

假如想和其它器材一同利用,或您和我一样是一个忘记的人,您能够会想到要正文代码。// 字符标明是正文,办事器不会处置被 // 标志的内容,也不会象 HTML 中的正文一样,把内容传给客户机。假如在 <?php 和 ?> 标志之间有尺度的 <!-- comment --> ,那末当办事器对它停止语法剖析时,有能够引发毛病。明显,您能够不会象我对这个根基功效一样正文您的代码,但它确是一个很好的示例。

最初要注重的是,每一个 PHP 函数都关闭在圆括号内,并以分号停止,这和 C 或 Perl 类似。因为一个复杂的印刷毛病而漏掉一个停止的圆括号或分号,形成一些语法毛病是很罕见的,所以要确保反省代码。在象 Vim 或 Emacs 如许的编纂器(可以凸起显示语法)中编写 PHP 是有助于消弭此类毛病。它使您能立即捕获到毛病。

date 函数仅是内置 PHP 函数之一。PHP 附带了很多功效可以用于数据库毗连、创立 PDF、Shockwave、JPG、GIF、PNG 和其它图像文件、发送电子邮件、浏览和书写文件、语法剖析 XML、会话处置、经过 HTTP 直接与阅读器对话,和很多其它功效。

PHP 也答应用户界说本人的函数。这使 PHP 言语可以经过 Web 供应大批的处理计划。而不是从一入手下手就把一切的工作都编写好了。在您编写函数之前,确保已检查诸如 Zend.com、PHP Wizard 固然还有 Freshmeat,来看是不是已有您正在测验考试编写的函数(请参阅参考材料)。

关于供应题目、主动更新旧事网站、基于 Web 的电子邮件客户机、数据库办理和其它方面,已有很多开放源码的 PHP 处理计划。再从头来做这些是毫有意义的。相反,应当从已构建好的基本入手下手,并把它定制到您本人的处理计划中。假如您仅仅是初步懂得和进修 PHP,在心中并没有详细的项目,那末这些项目关于利用 PHP 依然是很好的示例和进修材料。

停止语
这篇 PHP 剧本言语的扼要概述说明了 PHP 功效和用法。在今后的文章中,我会讲述经由过程会见 MySQL 数据库来创立静态的 Web 产物页面。在那之前,请检查上面关于 PHP 的参考材料以懂得更多信息。

参考材料

PHP.net 是次要的 PHP 网站。
PHP Wizards 包括一些文档和几个盛行的 PHP 项目。
作者 Julie C. Meloni 创立的 Thickbook.com 有很多有效的教程。Julie 是 PHP Essentials(最优异的 PHP 书本之一)的作者。
PHP Builder 是另外一个盛行的网站,它下面有一些教程和代码库。
Zend.com 包括大批有效的关于 PHP 的链接和信息。Zend 是 PHP 的优化器。
请检查 Freshmeat 来失掉关于 PHP 开辟的一些更新,和其它一些器材。
请到 Perl.com 懂得有关另外一个盛行的剧本言语的具体信息。
请检查 MySQL 官方主页。
请到 PHPMyAdmin 网站检查已处理的 PHP 和 MySQL 成绩。
官方的 Python 网站老是有效的。
Java Lobby 有很多有效的 Java 信息。
关于作者
Joe "Zonker" Brockmeier 是 Linux Magazine 的攥稿编纂,他为 Prima 出书社写过 Install, Configure and Customize Slackware Linux 一书。他的第二本书,DocBook Publishing,将由 Prima 于 2001 年早些时分出书。可以经由过程 jbrockmeier@earthlink.net 与他接洽。



以下为英文原文

=======================================================

Finally, the perfect language for dynamic content and database interaction

Joe "Zonker" Brockmeier
Senior Editor, User Friendly Media
December 2000

内容:


Background
Dynamic vs. static content
Platforms
Licensing and use
Using PHP
Summary
Resources
About the author



Joe Brockmeier presents a brief introduction to the PHP scripting language with a discussion of PHP's origins, capabilities, and the platforms it's available on. A simple PHP script example highlights basic syntax and usage.




If you work with Web-based development, you've probably heard about PHP. You might not know exactly what it is, how it works, or why it's so hot, but you do know it's time to find out more about it. So here's a quick intro to the basic concepts that underlie PHP.

A bit of background
PHP started out as a small open source project that evolved as more and more people found out how useful it was. Rasmus Lerdorf unleashed the first version of PHP way back in 1994. It has been picking up steam ever since and is now at version 4.0.3 with numerous improvements and refinements over the original release.

PHP is a scripting language that is embedded in HTML and interpreted by the server. It can be used to manage dynamic content, work with databases, handle session tracking, and even build entire e-commerce sites. It works well with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.

Dynamic vs. static content
What's so hot about dynamic content? Let's say you're managing an e-commerce site with 10 products. It's not that difficult to hand-code ten static product pages with all the requisite information, forms and such, provided your products don't change often and you don't anticipate much growth. However, let's say you add ten more products this month, and then more next month, and occasionally prices change or you want to change the look and feel of your site. Then you're stuck re-coding dozens, maybe hundreds, of static pages by hand.

On the other hand, let's say you start by creating one page that is called product.php. Instead of holding static information, it's coded to pull information out of your product database and build a page dynamically. You then have one meta page that can serve up one or one hundred or even a hundred thousand unique pages based on information stored in a database. Rather than requiring a Web master to spend an entire day doing nothing but monkey-work updating static Web pages, the information can now be updated at the same time the information is changed in the company database. You eliminate the headache-inducing lag between the time information is changed in the database and the time it makes its way onto the Web site.

At the risk of generalizing, for work on the Web, PHP is a great way to go. It's not the only way to go; Perl, Java, JavaScript, ASP, Python, Tcl, CGIs, and probably dozens of other ways are available for generating dynamic content. However, PHP has the benefit of being designed just for Web-based problems and of being an open source project.

If you're looking for a programming language for a word processor or 3D game, then PHP probably isn't the way to go. If you need to run a Web site with dynamic content, database interaction, and e-commerce, read on, because PHP is going to be very helpful indeed.

Platforms available for PHP
The most common PHP installation is probably the PHP module running with Apache on Linux or a UNIX-variant. But if you're using something else, don't worry. PHP works on Windows NT and 9x, as well as with a number of other Web servers. You'll find more documentation floating around on the Web that's specific to the Apache/Linux/PHP combo, but it isn't by any means the only platform that PHP is supported on.

Licensing and use
How much might you pay for a full-featured embedded Web scripting language? How about nothing? PHP is an open source project, so there are no licensing fees or restrictions on use. You could run a small, non-profit site, or you could run a billion-dollar e-commerce site with PHP, and the cost is the same: zero. Not only that, but the PHP code is available in case you want or need to tweak it.

PHP is not licensed under the GPL, but its own license permits redistribution of code and/or binaries.

Using PHP
Okay, so now you're convinced that you want to actually try PHP out. We'll walk through a few simple examples so you can get your feet wet. Keep in mind that this is by no means an exhaustive look at PHP, just a quick starter.

"Hello, World!"
To get a feel for PHP, let's look at some very simple PHP scripts. Since "Hello, World!" is an obligatory example, we'll produce a friendly little "Hello, World!" script.

As mentioned earlier, PHP is embedded in HTML. (You could have a file that contains almost no HTML, but usually it's a mixture.) That means that in amongst your normal HTML (or XHTML if you're cutting-edge) you'll have PHP statements like this:

<body bgcolor="white">
<strong>How to say "Hello, World!"</strong>
<?php echo "Hello, World!";?>
<br>
Simple, huh?
</body>






Simple, right? Just an "echo" statement, and that's it. Of course, that on its own isn't terribly useful. But it does teach us something about the language. (By the way, if you examine the HTML output, you'll notice that the PHP code is not present in the file sent from the server to your Web browser. All of the PHP present in the Web page is processed and stripped from the page; the only thing returned to the client from the Web server is pure HTML output.)

Printing date and time in a Web page
Now we'll do something a little more useful. This example will print out the date and time in a Web page.

<body bgcolor="white">
<strong>An Example of PHP in Action</strong>

<?php echo "The Current Date and Time is:<br>";
echo date("g:i A l, F j Y.");?>
// g = the hour, in 12-hour format
// i = minutes
// A = print AM or PM, depending...
// l = print the day of the week
// F = print the month
// j = print the day of the month
// Y = print the year - all four digits







This code produces the output:


The Current Date and Time is:
11:00 AM Friday, October 20 2000.

Notice the blend of PHP and HTML here. I'll assume that you can already read HTML, so I'll only explain the PHP code. You can find a complete PHP reference online at PHP.net (see Resources).

The PHP code begins with the tag <?php and ends with ?>. This tells the server that everything between <?php and ?> needs to be parsed for PHP instructions, and that if they're found, they need to be executed. Note that when your document is processed and served, it will be received by the client as plain HTML. Someone browsing your site will not see any of your PHP instructions, unless you've made an error and the server spits them out as-is instead of processing them first.

Regular HTML tags within the <?php and ?> will be processed normally. Note that the simple script above contains a <br> line-break tag. PHP wouldn't be very useful if you couldn't include HTML formatting as well.

If you're going to be working with others, or if you're just plain forgetful like me, you'll want to comment your code as well. The // characters indicate a comment, which the server will not process or pass on to the client, unlike comments in HTML. If you include a standard <!-- comment --> within the <?php and ?> tags, it is likely to cause an error when parsed by the server. Obviously, you probably wouldn't comment your code quite so much as I have above for such a basic function, but it makes a good example.

Finally, note that each PHP function is enclosed in parentheses and ends with a semicolon, which will seem familiar to fans of C or Perl. It's not uncommon to forget a closing parenthesis or semicolon and have a number of parse errors just because of a simple typo, so be sure to check them. It's helpful to edit PHP in an editor like Vim or Emacs that is capable of syntax highlighting. This allows you to catch your errors right away.

The date function is just one of the built-in PHP functions. PHP also comes with functions for database connectivity, creating PDF, Shockwave, JPG, GIF, PNG, and other graphics files, sending e-mail, reading and writing files, parsing XML, session handling, talking directly to the browser via HTTP, and many other functions.

PHP also allows the user to define their own functions. This makes PHP a language capable of providing a huge number of solutions via the Web. Rather than just writing everything from scratch, however, be sure to check sites like Zend.com, PHP Wizards, and, of course, Freshmeat to see if what you're trying to do has been done already (see Resources).

There are a lot of open sourced PHP solutions for serving banners, automating news sites, Web-based e-mail clients, database management, and much more. There's no sense re-inventing the wheel. Instead, start from the foundation that has already been built and customize it into your own solution. If you're just poking around with PHP to learn it and don't have a specific project in mind, these projects are still great examples of what you can do with PHP and serve as great learning resources.

Summary
This brief overview of the PHP scripting language should give you an idea of what PHP is capable of and how PHP is being used. In a future article, I'll walk through accessing a MySQL database to create a dynamic Web product page. Until then, check out the resources on PHP below for further information.

Resources

PHP.net is the main PHP Web site.
PHP Wizards contains documentation and several popular PHP projects.
Thickbook.com, by Julie C. Meloni, has a number of very helpful tutorials. Julie is the author of PHP Essentials, one of the best PHP books available.
PHP Builder is another popular PHP site with tutorials and code libraries.
Zend.com contains plenty of useful links and information about PHP. Zend is the PHP Optimizer.
Check out Freshmeat to get updates on PHP development, among other things.
Go to Perl.com to find out more about another popular scripting language.
Check out the MySQL official home page.
Go to the PHPMyAdmin site to see PHP and MySQL handled.
The official Python site is always useful.
The Java Lobby has lots of useful Java info.
About the author
Joe "Zonker" Brockmeier is a contributing editor for Linux Magazine and has written Install, Configure and Customize Slackware Linux for Prima Publishing. His second book, DocBook Publishing, will be published by Prima in early 2001. He can be reached at jbrockmeier@earthlink.net.
  培训的第二阶段,开始了PHP语言语法结构和应用的学习。
若天明 该用户已被删除
沙发
发表于 2015-2-4 12:27:30 | 只看该作者
本文当是我的笔记啦,遇到的问题随时填充
板凳
发表于 2015-2-9 22:35:29 | 只看该作者
个人呢觉得,配wamp 最容易漏的一步就是忘了把$PHP$目录下的libmysql.dll拷贝到windows系统目录的system32目录下,还有重启apache。
灵魂腐蚀 该用户已被删除
地板
发表于 2015-2-22 15:04:53 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
冷月葬花魂 该用户已被删除
5#
发表于 2015-2-22 23:51:59 | 只看该作者
说点我烦的低级错误吧,曾经有次插入mysql的时间 弄了300年结果老报错,其实mysql的时间是有限制的,大概是到203X年  具体的记不清啦,囧。
莫相离 该用户已被删除
6#
发表于 2015-2-25 22:01:29 | 只看该作者
建数据库表的时候,int型要输入长度的,其实是个摆设的输入几位都没影响的,只要大于4就行,囧。
柔情似水 该用户已被删除
7#
发表于 2015-3-7 18:14:45 | 只看该作者
使用 jquery 等js框架的时候,要随时注意浏览器的更新情况,不然很容易发生框架不能使用。
深爱那片海 该用户已被删除
8#
发表于 2015-3-15 10:55:14 | 只看该作者
兴趣是最好的老师,百度是最好的词典。
变相怪杰 该用户已被删除
9#
发表于 2015-3-16 22:31:39 | 只看该作者
我学习了一段时间后,我发现效果并不好(估计是我自身的问题)。因为一个人的精力总是有限的,同时学习这么多,会导致每个的学习时间都得不到保证。
再见西城 该用户已被删除
10#
发表于 2015-3-23 05:04:05 | 只看该作者
我还是强烈建议自己搭建php环境。因为在搭建的过程中你会遇到一些问题,通过搜索或是看php手册解决问题后,你会更加深刻的理解它们的工作原理,了解到php配置文件中的一些选项设置。
愤怒的大鸟 该用户已被删除
11#
发表于 2015-3-29 08:25:18 | 只看该作者
装在C盘下面可以利用windows的ghost功能可以还原回来(顺便当做是重转啦),当然啦我的编译目录要放在别的盘下,不然自己的劳动成果就悲剧啦。
老尸 该用户已被删除
12#
发表于 2015-4-12 05:59:07 | 只看该作者
多看优秀程序员编写的代码,仔细理解他们解决问题的方法,对自身有很大的帮助。
兰色精灵 该用户已被删除
13#
发表于 2015-4-15 04:16:42 | 只看该作者
先学习php和mysql,还有css(html语言很简单)我认为现在的效果比以前的方法好。
谁可相欹 该用户已被删除
14#
发表于 2015-4-16 23:34:42 | 只看该作者
微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox  支持的不是很好,所以能少用还是少用的好。
简单生活 该用户已被删除
15#
发表于 2015-5-4 12:47:38 | 只看该作者
最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。
飘飘悠悠 该用户已被删除
16#
发表于 2015-5-9 12:36:36 | 只看该作者
php里的数组为空的时候是不能拿来遍历的;(这个有点低级啊,不过我刚被这个边界问题墨迹了好长一会)
若相依 该用户已被删除
17#
发表于 2015-5-10 04:47:22 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
小妖女 该用户已被删除
18#
发表于 2015-6-9 07:16:27 | 只看该作者
刚开始安装php的时候,我图了个省事,把php的扩展全都打开啦(就是把php.ini 那一片 extension 前面的冒号全去掉啦),这样自然有好处,以后不用再需要什么功能再来打开。
admin 该用户已被删除
19#
发表于 2015-6-24 21:21:23 | 只看该作者
找到的的资料很多都是在论坛里的,需要注册,所以我一般没到一个论坛都注册一个id,所有的id都注册成一样的,这样下次再进来的时候就不用重复注册啦。当然有些论坛的某些资料是需要的付费的。
金色的骷髅 该用户已被删除
20#
发表于 2015-6-27 00:34:48 | 只看该作者
php是动态网站开发的优秀语言,在学习的时候万万不能冒进。在系统的学习前,我认为不应该只是追求实现某种效果,因为即使你复制他人的代码调试成功,实现了你所期望的效果,你也不了解其中的原理。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-11-14 13:48

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表