Saturday, April 16, 2011

Your First PHP Program

A PHP program is just a text file, like any other Web page. Most HTML editors don’t know about PHP, though, so it’s best to use one that does. That way, the editor will automatically alert you if it notices you’ve made an error in the code that you’re typing.
Thankfully, PSPad, which we used for creating our Javascript code, also knows about PHP. Hundreds of thousands of people across the world use PSPad as their PHP editor, so that’s what we’ll use too.
While Javascript files tend to have a .html or .htm extension, just like standard web pages, PHP files tend to end with .php instead. This isn’t a hard and fast rule. Some web servers will allow php code to exist within .html files, but many insist on you ending all your files with a .php extension. So, to keep things simple and consistent, we’ll stick to a rule of always using a .php extension on any file that contains PHP code.

Right, the time has come to write a PHP program. If you haven’t already installed PSPad, you’ll need to follow the instructions on page 180 and do so. With PSPad installed, launch the program. Click the FTP tab on the left hand side of the screen, then click the Connect FTP icon in the row below it, and choose your site name from the list that appears. Then double-click on the public_html folder, or whatever your host uses to store your web accessible files.
In the white space below the list of files and folders on the left hand side, right-click and choose New File. In the box where you’re asked to choose a file name, type demo.php and click the OK button. You should see your new file appear in the list in the left-hand panel.
Now double-click your demo.php file to open it for editing. Look at the blue title bar at the top of the screen, the text of which should end with demo.php rather than new1.txt to indicate that you’re editing the correct file. If not, double-click it again.
You can now start typing your first PHP program. Enter the following:

<?php
echo "<HTML>";
echo "<body>";
echo "This is some PHP";
echo "</body>";
echo "</HTML>";
?>
The screen should now look like this:


Save the file to the server (remember PSPad has built-in FTP so there’s no need to upload anything manually) by choosing Save from the File menu. After a couple of seconds, when the file is saved, you can close PSPad.
Remember that, unlike Javascript, PHP code gets run by the web server, so you can’t write and test PHP programs unless you’re connected to a Web server. Hence the need to upload our program.



Excellent. It worked. The web server recognised and ran our program. If you get an error message about syntax errors, go back and edit the file, and check your typing carefully. If you just see the PHP code itself, check that your web server really does support PHP and that it is enabled on your hosting account. It’s possible that you have to specifically request it, though this is unusual.
Although this is a very simple PHP program, it does demonstrate a number of key points about the language.
First, you’ll notice that the code starts with <?php and ends with ?>. These special markers tell the web server where PHP code starts and ends, rather like the <script> tag that marks the start of some Javascript. Earlier versions of PHP allowed simply <? at the start of a script, but later versions insist on the full <?php instead. So even if you see web-based tutorials or books that tell you it’s OK to use the shorter <? at the start, it’s not. Get into the habit of using the full version and you’ll be better off.

Next, notice the "echo" command. This is rather like document.write in Javascript, in that it creates content which is sent to the visitor’s web browser to display. In this case, we’ve created an entire HTML page consisting of <HTML> tags, <body> tags, and some text. Wehave, in web developer terminology, created a dynamic web page. There is no .html file on the server that contains this page. Instead, it has been dynamically created "on the fly", and sent to the visitor’s browser.
Not particularly impressive, sure, but I’ll show you some more powerful examples shortly.
Note that you don’t have to stick to pure PHP code within a php file. You can mix HTML and PHP code within the same file. For example, we could just as easily have written our program like this:

<HTML>
<body>
<?php
echo "This is some PHP";
?>
</body>
</HTML>
Here, we stay in HTML mode to create the first two tags. Then we switch to PHP mode to generate the text, and then back into HTML mode to generate the two closing tags. As you start to write longer PHP programs, deciding on which way to work is important.
There’s no right or wrong way – just do as you prefer. Some people don’t like the idea of flipping in and out of PHP mode 20 or 30 times within a complex file, whereas others prefer to stick in HTML mode and only use PHP mode where it matters. You’ll see more about this later, but for the moment you need to bear in mind that there are two ways of doing things.

There’s one more vital thing you need to understand about PHP, and indeed every serverside programming language. Assuming your web browser is still open, hit the View Source menu option and look at the HTML source code of the page. It looks like this:


Note how you can see the output of the PHP program, and not the PHP program code itself. The program was on the Web server, and the "echo" commands forced the HTML tags and other text to be sent to your browser. At no time does the PHP code itself leave the server, and at no time is it visible to the site visitor.
This is crucial to understand. It’s the difference between server-side and client-side programming. And it means that you can safely use PHP to implement security-related features, because no one can tamper with the code.
If we’d written this program in Javascript, as a series of document.write statements, using the View Source option on our browser would have displayed the Javascript program code.
But in this case, the browser never gets to see the code because it has already been run by the server.
One final point. Why are there no blank lines between each of the HTML tags, and before/after the text? Shouldn’t the dynamically-generated HTML code look more like this, rather than being all in one line?:
<HTML>
<body>
This is some PHP
</body>
</HTML>
The reason that there are no "carriage return" characters in our dynamically-generated HTML, is because our PHP program never created any. Sure, we put each line of PHP code on its own line, but that won’t cause carriage return characters to be sent to the browser.
It doesn’t actually matter. Browsers don’t insist on HTML code being neatly arranged in lines – it’s only done for the benefit of humans who might want to read the code. But if you did want to generate neat, line-spaced HTML, you’d have to adjust your PHP code accordingly.

Needless to say, there’s much more to PHP than the "echo" command. Like JavaScript, PHP is a hugely complex and capable language, which provides all of the tools you need to write sites such as eBay or Amazon. Perhaps the greatest benefit of knowing how to do server-side programming is being able to interact with a MySQL database, and we’ll come to this soon.
For now, though, the following chapter looks at some of the other things that PHP can do.
If you want to dive straight into PHP and experiment some more, the best resource is the official www.php.net site on the Web. Although the site can be confusing, it does contain everything you’ll ever need to know. Perhaps its best feature is that anyone can add their own comments and example to the online manual.

0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms