Chapter 1
Basic introduction to PHP
Key points of this chapter
◆ Understanding PHP
◆ History of PHP
◆ Reasons why PHP is popular
◆ Competitive advantages of PHP
First in the first chapter Give a basic introduction to PHP. This chapter focuses on discussing some of the most common questions about PHP, such as what is PHP, how does it compare to similar technologies, etc. Most of the chapter will be spent listing the reasons why PHP is popular. .
Get to know PHP
PHP is a web development programming language written by web developers.
PHP stands for "PHP: Hypertext Preprocessor". In fact, it was originally called "Personal Home Page Tools" at the beginning; however, as its use became more widespread, everyone in the online community recommended a newer and more appropriate name. (Just like the abbreviation GNU stands for "GNUᄄ@s Not Unix"). We can choose any file extension for the PHP file we write, but it is generally recommended to use . PhpᄄAphp3 and .phtml. Currently, the fourth version of PHP is generally used. The extension can be php4, or of course it can also be shortened to php.
PHP is a server-side embedded HTML scripting language that can be embedded in HTML or executed as a separate binary file (even though the former is often used). Corresponding products of the same nature include Microsoft's ASP, Macromedia's ColdFusion, and Sun's Java Server Pages (JSP). Because its functions and concepts are very similar to Microsoft's products, PHP is sometimes called "the open source ASP" (Open source version of ASP), although this formula has been misleading because PHP existed before ASP, but in the past few years, PHP and server-side Java have become more and more popular, although ASP has lost some usage, but such a comparison no longer seems appropriate.
The next chapter will explore the server-side scripting language in more depth. For now, readers can think of it as a super HTML markup that allows server-side functions to be added to the Web. For example, we can use PHP combination to generate a complex Web page, or use PHP program to trigger an automatically executed credit card payment process when customers shop online.
Strictly speaking, PHP has a lot to do with Web layout configuration, events or appearance design. In fact, most of PHP's functions are invisible to end users. When a user views a PHP web page, he does not need to distinguish whether the page is written in HTML syntax, because the final result of PHP is still HTML.
Apache HTTP server is the dominant free web server on the market. About 55% of the world's WWWs run it, and PHP is one of its official modules. In other words, the PHP scripting engine can be built into the web server, so it has faster data processing speed. Like Apache Server, PHP has cross-platform functionality, which means that it is originally executed in a UNIX environment, but it can also be executed on Windows. All software development projects supported by the Apache Software Foundation (http://www.apache.org/), including PHP, are open source software.
History of PHP
PHP is inherited from an old project called PHP/FI. PHP/FI was created by Rasmus Lerdorf in 1995 as a simple set of Perl scripts to track information about people who visited his homepage. It named this set of scripts "Personal Home Page Tools". As the need for more functionality grew, Rasmus wrote a larger C language implementation that could access the database and allow users to develop simple dynamic web programs. Rasmus releases the source code of PHP/FI so that everyone can use it, fix bugs, and improve the source code.
PHP/FI, a program designed to provide an interpreter for personal homepages/forms, already contains some basic functions of today's PHP. It has Perl-style variables, automatically interprets form variables, and can be embedded in HTML. The syntax itself is very similar to Perl, but it's limited, simple, and slightly jarring.
By 1997, PHP/FI 2.0, the second version of its C language implementation, had several thousand users (estimated) and about 50,000 domain name installations around the world, which is about 1% of all domain names on the Internet. But at that time there were only a few people writing a small amount of code for the project, and it was still just a one-man project.
PHP/FI 2.0 released the official version in November 1997 after several beta versions. Soon, the first alpha version of PHP 3.0 was released, and PHP became a success.
PHP 3
PHP 3.0 was the first version similar to today’s PHP syntax structure. While developing an e-commerce program for a university project, Andi Gutmans and Zeev Suraski discovered that PHP/FI 2.0 was clearly lacking in functionality, so they rewrote the code. This is PHP 3.0. After a series of efforts by Andi, Rasmus and Zeev, taking into account the existing user base of PHP/FI, they decided to jointly release PHP 3.0 as the official successor version of PHP/FI 2.0. Further development of PHP/FI 2.0 has almost ceased.
One of the most powerful features of PHP 3.0 is its extensibility. In addition to providing end users with the infrastructure for databases, protocols, and APIs, its extensibility also attracts a large number of developers to join and submit new modules. This turned out to be the key to PHP 3.0's huge success. Other key features in PHP 3.0 include object-oriented support and more powerful and coordinated syntax constructs.
This brand new language is released with a new name. It removes the implicit "this language is restricted to personal use only" part from the name of PHP/FI 2.0. It is named with the simple abbreviation "PHP". This is a recursive abbreviation, and its full name is - PHP: Hypertext Preprocessor.
In late 1998, PHP had nearly 10,000 installers, and about 100,000 websites reported that they used PHP. At its peak, PHP 3.0 was installed on 10% of web servers on the Internet.
After about nine months of public testing, PHP 3.0 was officially released in June 1998.
PHP 4
In the winter of 1998, shortly after PHP 3.0 was officially released, Andi Gutmans and Zeev Suraski began to rewrite the PHP code. The design goal is to enhance the performance of complex program runtimes and the modularity of PHP's own code. PHP 3.0's new features and extensive third-party database and API support make it possible to write such programs, but PHP 3.0 does not have the ability to efficiently handle such complex programs.
The new engine, called the "Zend Engine" (which is the abbreviation of Zeev and Andi), successfully achieved the design goals and was first introduced to PHP in mid-1999. PHP 4.0, which is based on this engine and incorporates more new features, was released as an official version in May 2000, two years after the release of PHP 3.0. In addition to higher performance, PHP 4.0 also includes other key features, such as: support for more web servers; HTTP Sessions support; output buffering; more secure methods of handling user input; and some new languages structure.
PHP 4 is the latest version of PHP currently available. Work has begun on modifying and enhancing Zend Engine to incorporate new features in PHP 5.0.
Today, 10,000 developers (estimated) and several million websites report having PHP installed, accounting for 20% of the entire Internet’s domain names.
The PHP development team has many excellent developers, and there are also a large number of outstanding talents working on PHP-related projects, such as PEAR and PHP document projects.
PHP 5
The future of PHP is primarily controlled by its core, the Zend Engine. PHP 5 will include the new generation Zend Engine 2.0. Please visit its website for more information about this engine.
History
The reasons why PHP is popular
There are many reasons why PHP is popular. Here are a few main reasons.
PHP is free
PHP itself doesn’t cost you a dime. Combining Apache/PHP/MySQL and running it on cheap low-end hardware is something you don't have to think about when you decide to adopt IIS/ASP/SQL Server.
For comparison, Table 1-1 lists the approximate retail prices of some similar products in the United States. All prices are based on a single external web server and are compared with the most suitable database and development tools. $0 represents an A cost-effective and actually commonly used option, in current use, Microsoft has not listed the NET Server Suite as the next generation Web server, but we can conservatively infer that prices will rise rather than fall.
Table 1-1 Retail Price Comparison (USD)
Open Source Software: Cheap and Good
Obviously, we live in a material world, and we often encounter maxims like "You get what you pay for", "There is no such thing as a free lunch" and "Perfect things only exist in legends" . Therefore, everyone will inevitably doubt the quality and survival of free software. Until recently, free software (formally known as shareware or free software) generally fell into one of three categories:
Small and non-commercial software
Software that does general low-level tasks User-written software for a socio-political "problem" should now radically change these stereotypes. There is no doubt that we are in an ever-changing world of business software. Nowadays, many important consumer software are given away for free: e-mail clients, web browsers, games, and even full-service office suites, as soon as their creators create a Web version or set up an FTP server. It will be published immediately and as quickly as possible. The meaning of user software is gradually changing, just like the sweetness of flowers attracts bees for pollination. In other words, it is used as a way to help sell more server hardware and operating systems, improve integration, and become an advertising channel. , selling some operational tool components or selling equity. Therefore, the retail price of software is no longer a reliable criterion for evaluating the quality of the software or its user level.
On the server side, open source products have become more powerful; not only can they compete with the best commercial software, but in many aspects people feel that they have surpassed commercial software. Don't think this is just our opinion, go ahead and ask IBM, all the hardware manufacturers, NASA, FTE, Siemens, Kinko, the Queen of England, the Mexican education system. If you still have doubts about this, you can learn more about it from the following websites:
http://www.opensource.org
http://www.fsf.org
PHP authorization
Free and open Source code and free software are guaranteed by a series of licensing structures, the most famous of which are the GPL (GNU General Public License) or (copyleft). PHP used to be distributed under both the GPL and its own license, allowing each user to freely choose one. This has changed, however: the program as a whole is now publicly distributed under its own ultra-laissez-faire PHP4 license: Zend is licensed as a separate product under the Q Public License (the Zend was priced and sold publicly when it was forked from PHP.
http://www.php.net/license.html
http://www.troll.no/qpl/annotaed.html
Most Most people get PHP as a free download, but it can also be purchased as part of a Linux distribution, a technical book, or some other product. In this latter case, some people may doubt the claim that PHP is free. This can be seen in two ways. Although you cannot require payment for most open source software, you do need to pay for shipping the software, even in the cheapest way, such as storing it on a disk and (mailing or using other tools) to other users. In addition, some work or risks are completed and borne by the market, but these tasks are not done or borne by the development team. In this case, users also need to pay. For example, in order to ensure that every copy of the software you receive is virus-free or of a certain quality, or in order not to risk using a CD-ROM with a virus, you need to pay a certain fee.
Other open source products, such as Linux, have companies like RedHat answering user questions: Business operations in this area have just begun for PHP.
PHP is easy to get started
Compared with other operating methods for the same function, PHP is very easy to learn. Unlike Java Server Pages or CGI based on the C language, PHP does not require users to learn an entire programming language just to make a small database access call. Perl is often half-jokingly called a "write-oniy language". Unlike Perl, PHP's syntax is easy to understand and the human-computer interface is very friendly. Unlike ASP.NET, PHP is already very stable and can solve your current problems.
In addition, PHP also pre-defines many very useful functions for users (such as opening a connection to an Oracle database, or fetching emails from an IMAP server). When learning PHP, you can also see many complete and ready-made sample scripts. In fact, when you want to use PHP, you can completely modify these existing script programs without wasting time on some small syntax errors encountered when writing programs.
In order to prevent readers from misunderstanding, it must be explained: "Easy" may have different interpretations for different people and different situations. For example, for many web developers, "easy" seems to mean Next is the graphical interface, drag-and-drop operation of the mouse and WYSIWYG environment. To become a truly proficient PHP user, you first need to be able to edit HTML with ease. We can first use WYSIWYG web editing tools to design the website, beautify the page and insert client functions, and then add the PHP function to the source code of the page. Some methods can even be introduced in detail. In any case, if you don't want to touch the program code part, it is impossible to fully master the various functions of using PHP.
The vast majority of advanced users of PHP (including most of the development team members) are die-hards who write program code by hand. They have some insistence on programming work. For example, they think hand-written program code is very beautiful. It is clean and tidy, and has maximum compatibility with browsers. Therefore, handwritten program code is the only way to go, and people often do not hesitate to use fierce words to express this. Some PHP community groups provide assistance via email or conduct small technical transactions. If you want to join these communities, you must be able to quickly parse plain text original program code. Some WYSIWYG editor users sometimes ask members of the PHP community to help diagnose errors by viewing their web pages, but few get good reviews.
We have mentioned a lot of reasons why PHP is very simple. In fact, it is just a little more difficult than HTML, but a little simpler than JavaScript or ASP. In addition, there is absolutely no copying of JSP or ASP.NET.
PHP is embedded
PHP is embedded in HTML. In other words, the PHP page just "jumps" to the ordinary HTML page in PHP mode when necessary. Here is an example:
Example 1-1 greeting.php
When a client makes a request for the page, the web server will process it. That is, it will look from top to bottom in the page for the part of PHP it is trying to solve. First, the parser will process all variables with assigned values (marked with $) and try to insert them into subsequent PHP commands (in this case the print() function). If every step goes smoothly, the parser will eventually send an HTML page back to the client's browser, as shown in Figure 1-1
If you view the original program code from the client browser [Select Source (view source code) or PageSource (web page source code) from the View menu], you will see the following program code:
This is exactly the same as the HTML you wrote. Isn’t it very simple?
PHP's HTML embedding feature can provide a lot of help:◆.
◆ PHP separates the work of designers and script writers.
◆ No line of HTML has to be rewritten in a programming language.
◆ PHP can reduce labor costs and improve efficiency.
Perhaps the most attractive thing about all embedded script languages is that "just write it and execute it", there is no need to edit and translate it into binary code before being tested or used. PHP is a literal language (like most modern computer programming languages today). Although Zend does some work behind the scenes to pre-compile it into an intermediate format to improve the execution speed of complex scripts.
PHP can run on any of the most popular UNIX (including Mac OS X) or Windows operating systems. Currently, most of the HTTP servers in the world are built on the above two operating systems.
Table 1-2 Operating systems and web servers that support PHP
Now PHP can be executed on Macintosh, so PHP can cross almost all platforms. You can use your favorite tools to develop on any separate OS side, and then Upload these PHP scripts to a UNIX or Windows server. Chapter 3 will detail this part of the process.
PHP is not a markup language
PHP is a real programming language. In comparison, ColdFusion is marked with predefined tags like HTML. In PHP, you can define functions according to your needs by just typing the name and definition. In CouldFusion, you must use tags developed by others, or go through the development process of custom extended tags.
A PHP community member once said: "CouldFusion makes easy things easy, but makes medium-difficulty things impossible." Every programmer will agree that once you have the ability to use braces and circles, you will never go back to using markup.
PHP stability (stabie)
The [stability] mentioned here has two different meanings:
1. The server does not need to be restarted frequently.
2. The software does not need to be upgraded and changed frequently, and there will be no incompatibility between releases.
Happily, both of these points apply to PHP. Apache Server is generally recognized as the most stable mainstream web server, with an enviable uptime ratio. Although it is not the fastest, it is not the easiest for administrators, but once it is installed and set up, Apache HTTP Server rarely fails. There is also no need to restart the server each time a setting is changed (at least on the UNIX side). PHP inherits this reliability, coupled with its own robustness and lightness. In a two-and-a-half-month real-world test at Network Computing Labs in October 1999, Apache Server and PHP easily outperformed in terms of environmental stability. Beat the IIS/Visual Studio combination and the Netscape Enterprise Server/Java combination.
In terms of functionality and stability, PHP is also very good. The development team is confident in its project and has a clear goal, so it will reject inappropriate requests from some users. Most of their efforts have improved the product, such as making the parser run faster, exchanging data with more mainstream databases, or adding better Session support. Judging from past experience, few features have been removed between versions of PHP.
PHP is very fast
PHP is very fast in execution, especially when compiled into the Apache module on the UNIX platform.
Compared to CGI script, PHP4 is much faster in almost every aspect of use. There is a joke that CGI is the abbreviation of [Can't Go Instantly]. Although many CGI scripts are written in C, which is one of the lowest stage languages and therefore much faster than most programming languages, each request cut off from the HTTP server requires the start of a completely new handler , this problem hinders the application of CGI. The time required to cut and generate resources is considerable, limiting the amount of parallel processing that can be performed. Other CGI scripting languages, such as Perl and Tcl, are quite slow. Most Web sites are no longer using CGI for performance and security reasons.
Because it is executed literally rather than compiled, the performance is slightly affected. However, PHP, which is regarded as one of the web server modules, still has more advantages than disadvantages, and it is still worth it. When compiled this way, PHP becomes part of the HTTP server. Because there is no round trip to a separate application server (as is the case with ColdFusion), requests can still be served to the maximum extent possible.
Although there are no official benchmarks to compare the two sides, there are many indications and small benchmarks that PHP is at least as fast as ASP in most applications (for example, please connect to the Zend.com website for actual Feel it).
PHP is open
We have already discussed the cost advantages of open source software. Another major consequence of these licenses is that the complete source code of the software must be included in any distribution.
In fact, the UNIX version of PHP is only released in the form of original program code. Until now, the development team has rejected countless requests and is determined not to release an official binary version for any UNIX platform. At first, novice users (especially those who are new to UNIX) may feel that the original program code is like using a third leg [so easy to use], and most people still look forward to using the convenient rpm method. However, having a folder full of [.c] and [.h] files is unavoidable for both practical and ideal reasons.
The most direct and substantial benefit is that we can compile PHP for the content we really need. This method has advantages in terms of speed and security. For example, you can choose to hang up the interfaces of different databases, or recompile at any time as needed when a more secure or newer version of Apache comes on the market. In addition, you can also compile this function when you determine that you need XML support. .
What separates open source software from its competitors is not just price, but also control. Many software are set up to give up control under certain conditions, but take a closer look at the relevant In the authorization instructions, you will usually see [restrictions] on how to use the software. It might be allowed to run it at home, but not in the office; it might be allowed to load it on a laptop, but it would be a violation if used for business purposes. Or, most commonly, it can be used for any purpose, but the user never wants to see the original program code, let alone modify it. There are even community licensing regulations that force users to contribute to the link library, but users are still required to pay for the use of the final product!
Don’t think about how to violate software authorization, especially for these free-to-pay software, there is no good reason to violate this rule. In addition, for software developers, this kind of reward is too unfair.
PHP has excellent integration capabilities
PHP quickly adds new features
The development of PHP is also quite consistent and ongoing, although it is still affected by some major issues, such as the transition between PHP 3 and PHP 4. These are mainly caused by the parser itself, and programmers actually change between versions. Most of the extension components will not have big problems. In addition, the PHP team follows the principle of "release early and release often" of open source code, which gives developers many opportunities to make changes and make bug suggestions. This is relatively easy. If you look at the .NET approach, you will find that it has made developers wait for Microsoft for nearly a year but it has not yet updated IIS and has not even released a major version of the .NET server.
PHP is becoming popular and growing
Since the birth of PHP in 1994 to now, PHP has changed from an excellent tool for network development to a complete platform suitable for enterprise configuration, and it is changing. It is becoming more and more popular and has a huge resource base. Currently, PHP is used by more than 15 million websites. PHP itself has quickly become the most popular language for network applications. Especially in Europe and North America, PHP has been used in major industries including finance, government, transportation, telecommunications, media, entertainment, etc., completely breaking away from the realm of a simple online language development tool.
Abroad, millions of professional engineers use PHP as their main development language, forming clubs or similar groups in different regions, and often discuss issues of concern about PHP together. For example, in recent times, there have been some active activities in Belgium, the Netherlands, and the United Kingdom in Europe, as well as in the United States and Canada in North America. As the largest Internet country, China has many PHP enthusiasts, which can be said to be more than any other country. I hope that we can all unite and advocate the establishment of the largest PHP China in China. In this way, we will promote the development of PHP in China and provide a communication platform for a wide range of PHP enthusiasts.
As you can see, from October 1998 to October 1999, PHP users showed an 800% growth rate in the number of domains. As Web sites become ubiquitous, and most of them are no longer just simple static HTML pages, PHP is expected to quickly gain an overwhelming number of users.
Although it is difficult to determine a strong position, PHP is indeed in a strong position relative to similar products. 24% of web servers use Microsoft's Active Server Pages technology, while about 4% of the domains surveyed use ColdFusion. According to a larger and more accurate sample, 24% of all web servers use PHP. Therefore, it can be called the most popular server-side web programming language.
Active Server Pages and ColdFusion are easy to see as many large e-commerce websites choose to use them. However, most Web websites provide information in the form of information rather than revenue or national government, so they cannot cope with the high development costs. In this type of website, PHP enjoys a more substantial competitive advantage than its competitors.
PHP does not belong to anyone
Most of the progress of the PC industry can be said to be the chronicle of "patent rights standards": first try to establish a foothold, then collide with each other in their own interests and debts to customers, and finally end with New standards replace...
However, over the past few years, the Internet has provided the greatest tolerance for spontaneity, standards-based, platform-independent compatibility... and other characteristics. For example, e-mail is so widely used because objective existence uses a clear and fixed standard, and all programs on any platform must comply with this standard. Later new development projects that wanted to break this standard (for example, e-mail stationery based on HTML Green) were generally considered to be deviations from the standard.
In addition, customers (especially large customers with huge systems) tend to spend a lot of money to comply with proprietary standards. This is just because the market trend is not guaranteed, and the current method is driven by XML and Web services. This is Because for many years customers have been dissatisfied with Java RMI, CORBA, COM, and even earlier proprietary methods and data formats.
Currently, software developers are trying to integrate patent rights and open standards. On the one hand, the company must ensure the maintenance of profits, and at the same time, it must adopt open standards. There are some major legal conflicts related to patent rights standards, and these are still being resolved. It could end up making changes to the underlying source code, potentially affecting the future survival of the companies involved. Faced with these uncertainties, being able to predict future solutions to such problems will attract a large number of current manufacturers.
PHP has the greatest flexibility precisely because it is against patent rights. It is not tied to any server operating system, unlike Active Server Pages. It is not tied to any proprietary cross-platform standard or middleware, unlike Java Server Pages or ColdFusion. It is also not tied to any browser, programming language or library. PHP does not follow the script and only cooperates with open source software. This practical attitude of independence and cooperation will help PHP develop more smoothly in the development field.
PHP Community PHP is developed and supported by user communities around the world in a cooperative manner. There are indeed some monster-level programmers with a lot of ideas and enthusiasm. Their fanaticism has indeed completed most of the work. Contribute your best ideas and maintain good relationships with the majority of users.
For most beginners, the main benefit is free, borderless and unconditional technical support. People on mailing lists, like 7-Eleven convenience stores, are available 24/7 to answer questions, help with code debugging, and listen to your complaints. This kind of support is more humane and more real. Members of the PHP community may ask you to read the manual, forward questions to the answered mailing list in the database, or ask you to complain, but they will never ask you to cut off the C slot or ask you to pay. Often they'll look at your code and tell you what's wrong, or they can even help you design an application from scratch.
When you become more familiar with PHP, you may want to give back and contribute. You can track bugs and provide suggestions to others on the mailing list, post your program examples to a public repository for others to access, write related documents to share knowledge with others, and of course you can also write C or C++ code; these are all ways to give back to the community.
Summary
PHP is not the magic key to solving every web development problem, but it has many advantages. PHP was built by and used by web developers. There are also many enthusiastic communities providing support. They are very powerful, reliable and very easy to use. It provides the best form of connectivity for every type of backend material. Didn't we forget to mention earlier that it's free? To understand PHP, it's best to use it in many of the most common web development projects.
The above is the content of the PHP Learning Guide - Chapter 1. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!