By: John Hanney Introduction To PHP PHP is a general-purpose scripting language that can easily be embedded into HTML and enliven your Web pages. PHP can give pages a more interactive or customized flare than it's possible with straight HTML coding. In the following paragraphs, you will only be tackling the server-side scripting portion of PHP. That's the code that communicates to the server and helps produce those wonderful dynamic Web pages you'll soon be creating. To obtain the history on PHP or a more
The Preparation Before you dive right in, you'll need at least a very basic understanding of HTML and how to upload Web pages onto your site. This would include transferring files using FTP and a familiarity with Telnet or SSH to rename files or set file permissions. Once you know you have a grasp on handling HTML pages, adding PHP to the picture should be easy and exciting. A PHP Tutorial - Short & Simple I'm ready, how do I get started? Let's begin by introducing the tags that denote a block of PHP code. You can do that in two ways:
You can write code with or without the letters The first function we'll try is echo. What does echo do? You guessed it. It displays whatever we tell it to the screen. Here's how we display the word hello to the screen:
By placing the word hello between
The only difference between this code and the code above it is that we assigned the hello string to a variable first, and then we displayed the variable. We can go one step further and display using a combination of strings and variables together. Here's some code that demonstrates string text with a variable.
Up to this point, you now have enough of the basics to apply some code to an HTML page. In the next section we'll demonstrate how you can put this new knowledge into practice. Of course this is a very brief tutorial, so there's much more to PHP than displaying strings and variables. Most tutorials start out in a similar fashion by presenting examples using echo as we have done here. After that, the tutorials vary in style and direction. Be sure to see some of the different tutorials that are available to you out on the Web when you've read through the one here.
Adding PHP Code To A Web Page A great way to get a feel on how PHP works is to see it in action. Let's create a Web page that displays the IP address of the current visitor. First, create a simple HTML Web page using a text editor you are most familiar with. On the Windows/PC platform this may be Notepad or Wordpad (from your Accessories folder), or on the Unix/Linux platform this may be vi (visual editor) or pico. Start the editor. Then copy and paste, or key in, the text from figure 1 below.
Now if we were to save this code just as it is and display it from the browser it would look something like this:
You've got to admit, that page in figure 2 would be a pretty boring if we were to leave it that way. Now, let's replace that string of zeros with some PHP code to make those numbers reflect the visitor's IP. We will introduce a system variable to help accomplish this. The new code is highlighted in yellow.
The next step is to save this page. But instead of saving it as an ".htm" or ".html", give it an extension of ".php". (Maybe you can call it "my_first_page.php".) Upload the page to your website insuring that the file is set with the same permissions as any HTML page would normally have. Then display it with your browser:
That's all there is to it. How's it look? It's probably not the most exciting piece of work you've done, but enough to inspire many more. If you'd like to walk through another example that's a bit more advanced and contains some code that's somewhat unique to DRH Internet, see our page entitled "Adding PHP Code To A Web Page: Supplement" Additional Online PHP Developer Information There's quite an abundance of PHP information just a click away. Many sites dedicated to PHP have great sections for the beginner. For another good introduction to PHP and it's basics, visit CNet's "Builder.com" website:
Zend Technologies has something for the beginner and more. Webmonkey reveals some information for beginners through Tim Ziegler's article:
Once you've got a handle on the basics, you'll need to look up and try some of those advanced functions. An online PHP reference is just the place to go. "PHP.net" is one of the homes of PHP:
Do you have a thirst for more? Then visit the place for the serious PHP coder:
Is there anything we missed? Sure. We just can't include them all here. But Yahoo has done a good job. Check this out on an occasional basis for the latest links on the topic:
|
||||||||||||||||||||||||||||||||||||||||