首页 > 代码库 > PHP: The big picture

PHP: The big picture

Before getting down to to the business of writing some real PHP, I want to take a few minutes to explain some basics. PHP needs to be processed by the web server before the page can be viewed in a browser. So, as a rule, you need to store all pages inside the server root. In most cases, this is a folder called htdocs. Although, it sometimes called www, www root, or public html. There are cases were PHP files can be stored outside the server root for security reasons. But that‘s something you can learn about at the later stage. The server relies on the file name extension to know whether to process a page as PHP.

Unless you‘re hosting a company or server administrator tells you otherwise, always use .php. You can mix pages with the .html file name extension in the same site. But it‘s a good idea to use .php for all files, even if they don‘t contain PHP code. By doing so, you can add PHP code later without needing to change the URL. PHP code should be enclosed in PHP tags. The opening tag consist of an opening angle bracket, question mark,and PHP. There‘s no space between any of the characters.

In some scripts, you‘ll find just the opening angle bracket followed by the question mark, without the PHP. This is a shortened version. It works on a lot of servers, but isn‘t guaranteed to work everywhere. So, I recommend that you should always use the full opening PHP tag. The closing tag is a question mark followed by a closing angle bracket. Now, in some cases you can omit the closing tag. But if there‘s any HTML or anything else on the web page other than PHP code, the closing tag must always be there. If in doubt, I recommend always using a closing tag. PHP can look daunting when you start out.

But you‘ll quickly recognize the common features. Variables act as place holders for unknown or changing values. Arrays hold multiple values. You use conditional statements to make decisions and loops to perform repetitive tasks. Functions and objects perform preset tasks. (JS亦是如此,这些语言有什么共通点吗?)When viewing PHP pages, it‘s important to remember that the PHP code must be processed by the web server. If you‘ve got into the habit of double-clicking HTML pages to view them locally, that won‘t work with PHP.

What will happen is that you‘ll either just see the raw code or you‘ll be prompted to download the file. So, it‘s important to remember that the server is running. It may sound obvious, but it‘s easy to forget, particularly in the early stages. And you should always view the page using a URL. So, in most cases, this will be HTTP:// local host, followed by wherever you‘ve put the file. Remember those basic points, and you‘ll soon discover that PHP isn‘t all that difficult to learn and is great fun to use.

PHP: The big picture