PHP for the World Wide Web: Visual QuickStart Guideby Larry Ullman
This visual, task-based guide gets users up and running with the basics of PHP, starting with basic syntax, testing/i>
Unlike those other professional-level books on open-source scripting, PHP for the World Wide Web: Visual QuickStart Guide isn't just for professional programmers--it's written for the rest of us with a passing familiarity with HTML.
This visual, task-based guide gets users up and running with the basics of PHP, starting with basic syntax, testing scripts, working with variables, creating Web applications, and more. Throughout the book, sample scripts and projects show the reader just what they need to know, and a companion Web site offers every script available for downloading.
Read an Excerpt
1: Getting Started with PHPLearning any new programming language should always begin with an understanding of the syntax you will use, and that's what we'll explore in this chapter. I will primarily cover the fundamentals, but will also include some recommendations that will improve your work in the long run. Failure to follow the rules of syntax is an all-too-common cause of errors in your code.
By the end of this chapter you will have successfully written and executed your first PHP scripts.
Basic SyntaxIn order to create your first PHP page, you will start off exactly as you wo uld if you were creating an HTML document from scratch.
There are two main differences between a standard HTML document and a PHP document. First, PHP scripts should be saved with the .php extension (e.g., index. php). Second, you wrap your PHP code with the <?PHP and ?> tags to indicate what is PHP as opposed to what is HTML.
To create a new PH P script on your computer:
1. Open a text editor such as SimpleText, WordPad, or whichever application you prefer. 2. Choose File > New to create a new, blank document.
3. Type <HTML> <HEAD> <TITLE>First PHP Script</TITLE> </HEAD> <BODY> (Script 1.1). You can put each element or element group on its own line to make it neater.
4. Type <?PHP on its own line.
5. Press Return to create a new line and then type ?>.
6. Type </BODY,</HTML>.
7. Choose File > Save As. In the dialog box that appears, choose Text Only (or ASCII) for the format.
8. Choose the location where you wish to save the script.
9. Save the script as first. php.
Check with your ISP to learn which file extensions you can use for PHP documents. For this book you will use .php, although you may be able to use .phtml instead. Servers still running PHP version 3 commonly use .php3 as the default extension. A file extension tells the server how to treat the file: file.php will go through the PHP module, file.asp is processed as ASP, and file.htmi is a static HTML document.
You can also check with your ISP to se if short tags (using <? and ?> instead of <?PHP and ?>) or ASP tags (<% and %>) are acceptable. Programs like Macromedia Dreamweaver can work better with PHP pages if you use ASP tags.
Sending Data to the BrowserNow that you have created your first PHP script, it's time to make it actually do something. As discussed in the introduction, PHP tells the server what data to send to your Web browser. For starters, you will use the phpinfo()function to create our data. This function, when called, will send a table of information to the Web browser itemizing the specifics of the PHP installation on that particular server.
To add the phpinfo() function to your script:
1. Open your first. .php script in your text editor, if it isn't already.
2. Put your cursor between the PHP tags (<?PHP and ?>) and create a new line by pressing Return). 3. On the new line, type phpinfo();.
4. Change the title of the page by replacing First with Test in line 3 of the HTML (Script 1.2).
5. Save your script as test. php.
Everv statement within PHP code must end with a semicolon (;). Forgetting to do so is a common cause of errors. You can put multiple statements on one line, with each separated by its own semicolon. For the sake of clarity, however, I would not recommend it.
A statement in PHP is an executable line of code, like print) or phpinfo(). The semicolon concluding these lines are the equivalent of telling PHP to go ahead and execute the command. Conversely; comment lines, the PHP tags, control structures (conditionals, loops, etc.), and certain other constructs I'll discuss in this book do not merit a semicolon. Each of these aspects of PHP do not do anything in and of themselves so much as dictate the circumstances for the statements to follow. That is to say: the PHP tag only indicates that PHP code is to follow; comment characters render text moot, and so forth. Thus, in general, a semicolon concludes a specific action, while no semicolon is required for constructs that create conditions.
For better or worse, PHP is rather liberal when it comes to case sensitivity of built-in functions like PHPINFO(). PHPinfo() and PHPINFO() will net the same results. Later in the book (for example, Chapter 2; Variables) you will see examples of instances where the word case will make a crucial difference. HTML, in contrast, is entirely case insensitive.
Phpinfo() is an example of a built-in function which comes standard in PHP To learn more about functions and how to create your own, see Chapter 9, Creating Functions.
You will find it handy to have a copy of the test. php file around. You can use this to check the PHP capabilities of a new server or see what features are supported, such as databases, GIF building, etc. You can also use this file to experiment with different extensions and learn which ones the server will process correctly and which it will not.
Testing Your ScriptUnlike HTML, which can be tested on your own computer using a Web browser, in order to see what the output of your PHP script will look like, it needs to be saved to a PHP-enabled server. If you are working directly on a server, the script is already there once you've saved it. If you are creating your script using a text editor on your home computer, you will need to use FTP (file transfer protocol) to place it on the server. Your ISP or Web host should provide you with FTP access. You will also need an FTP application such as Fetch (for the Macintosh) or WS FTP (for Windows).
To FTP your script to the server:
1. Open your FTP application.
2. Connect to the server, using the address, username, and password provided to you by your ISP or Web host (Figure 1.1).
3. Find the proper directory for your HTML pages (e.g., www/ or htdocs/).
4. Save your script (test. php) to the server. (As a rule, most FTP applications save transferred pages to the server with the same filename you are using for those files on your computer. If your particular FTP program gives you the option to specify the filename, use test. php....)
Meet the Author
Larry Ullman is the Director of Digital Media Technologies at DMC Insights, Inc., and also serves as the lead PHP programmer. He specializes in developing dynamic Web sites and Web applications and also teaches PHP through various mediums.
and post it to your social network
Most Helpful Customer Reviews
See all customer reviews >