Join David Powers for an in-depth discussion in this video PHP: The big picture, part of Learning PHP.
- Great! So you've decided to learn PHP. Before you can do anything useful, you need to learn the basic grammar or syntax of the language. A small amount of time spent learning the basics will more than repay itself in the end, saving you hours of frustration and confusion. Let's start with a broad overview of what PHP is and what it does. PHP stands for PHP Hypertext Preprocessor, it's a rather ugly name for a very powerful scripting language that's relatively easy to learn.
You can use PHP in your websites for a wide range of tasks such as process and email online forms, upload files from a webpage, automatically generate thumbnails from a larger image or watermark them. PHP can also read and write files directly on your web server. And that's not all. You can use it for date and time calculations that take into account different time zones and whether it's daylight saving time.
However, if you use PHP on your webpages, the server sends the page to the Zend engine that powers PHP for processing before sending the response to the browser. And if a database is involved, the Zend engine is responsible for sending the request of the database and processing the results. All these usually takes only a fraction of a second, so there's no noticeable delay. PHP was designed as an embedded language. What that means is that PHP code is often mixed or embedded in HTML markup.
This is a basic feedback form and it's got PHP commands mixed in with the HTML code. So immediately after this h1 tag on line 23, the following line has a block of PHP code, that's followed on line 25 by more HTML. This is a paragraph with a class warning and it says "Please fix the items indicated", and on the following line, line 26, is another small block of PHP code. So let's see what happens to this paragraph when we load the page into a browser.
Paragraph is completely hidden, it's been hidden by those PHP commands. But if I try to submit this form without filling in the fields, let's do that, the paragraph is now displayed as are these other error messages. But let's take a look at the page's source code. There is that h1 tag and it's immediately followed by the paragraph with the class warning, "Please fix the items indicated". There's no PHP code inside at all, it's just HTML.
That's because PHP is a server-side technology. The PHP code remains on the server and after its been processed or passed, it outputs text and HTML. PHP can look daunting when you start out. But you'll soon become familiar with common features that are used in most PHP scripts. Variables act as placeholders for unknown or changing values. Arrays hold multiple values rather like a shopping list.
You use conditional statements to make decisions and loops to perform repetitive tasks. Functions and objects perform preset tasks. When using PHP in a website, use the filename extension .php for all webpages. Although you can mix pages with the .html filename extension in the same site, 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 normally needs to be enclosed in special tags. The opening tag consists of a less than symbol, a question mark and php without any spaces between the characters. Don't use the shorthand opening tag that omits the letters php, it's not supported on all servers. The closing tag is a question mark immediately followed by a greater than symbol.
The closing tag is essential if your PHP script is embedded in HTML. But if can be left out if there's no other code following your PHP script. 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 folder. In many cases, this is a folder called htdocs, but the name can vary. On some servers, it's called www, wwwroot or public_html.
There are cases where PHP files can be stored outside the server root for security reasons, but that's something you can learn about at a later stage. The need to process the PHP script affects how you view PHP files locally. Don't try to double-click a PHP file in Windows File Explorer or the Mac Finder, it won't work. You'll either just see the raw code or you'll be prompted to download the file. Make sure that your local web server is running.
And always view the page using a URL. If you're using a similar setup to me, that means http://localhost/introducingphp/ and that's followed by the location and name of the file within the introducing PHP folder. So that's a broad overview of PHP. Let's get on with the practical details.
- Naming variables
- Storing text as strings
- Doing calculations with PHP
- Using conditional statements to make decisions
- Creating custom functions
- Deciphering error messages
- Emailing the contents of an online form
- Dealing with multiple-choice form fields