- Shopping Bag ( 0 items )
By the end of this chapter you will have successfully written and executed your first PHP scripts.
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.
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.
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....)
|Ch. 1||Getting Started with PHP||1|
|Ch. 3||HTML Forms and PHP||43|
|Ch. 4||Using Numbers||67|
|Ch. 5||Using Strings||85|
|Ch. 6||Control Structures||113|
|Ch. 7||Using Arrays||155|
|Ch. 8||Creating Web Applications||189|
|Ch. 9||Cookies and Sessions||239|
|Ch. 10||Creating Functions||271|
|Ch. 11||Files and Directories||297|
|Ch. 12||Intro to Databases||343|
|Ch. 13||Regular Expressions||385|
|App. A: Installation and Configuration||409|
|App. B: Resources||429|
Posted May 5, 2010
I have just begun a PHP class and this book has everything in it required for the class and then some. It not only explains the language, but also shows how it s to look in the code, and why it is there.Was this review helpful? Yes NoThank you for your feedback. Report this reviewThank you, this review has been flagged.
Posted December 14, 2006
Very straight forward book, and reads at your own pace. This book, and 'PHP and MySQL for Dynamic Web Sites (2nd Edition) are excellent for the beginner, novice, and even the advanced. The companion web site is also very helpful, and the Author has an included support forum where he will personally answer your php/mysql questions online.Was this review helpful? Yes NoThank you for your feedback. Report this reviewThank you, this review has been flagged.
Posted September 4, 2004
I found this book very clean, cut, and to-the-point. It was very descriptive and great for beginners, but in some cases, not for novices. I recommend it for aspiring PHP writers. It brings you from beginner to about advanced level, which will fit your needs in most cases.Was this review helpful? Yes NoThank you for your feedback. Report this reviewThank you, this review has been flagged.
Posted December 1, 2009
No text was provided for this review.