PHP: Lesson 1 "Hello World"

Programming tutorial

Lesson 1 of a multi-page tutorial on the PHP Programming Language.

Authors

Written 2010 by Will Johnson for Fast Forward Technologies
Email Fast Forward Technologies at fft2001@aol.com
or post your comments for public view far below.
Creative Commons Attribution 3.0 License
fft2001
The Knol Self-Tweeter on the left, is designed to work only for an article's author.

Follow Fast Forward Technologies on Twitter!
or use my Knol Public activity feed

Buy your own Knol Self-Tweeter for only two dollars! I will email it to you, when I receive payment.
What the Knol Self-Twitter does exactly is explained at this link.


PHP is a programming language that was specifically designed to work inside a web server.  This means, that if you have a web site already, which you pay for or which runs locally (on your own computer), it most likely has PHP installed and accessible.  You just have to know how to write programs in that language and a whole new world of amazing tricks is open to you.


There are three ways that you might have a web site.  Most mid-to-large companies run their own web sites, on their own web servers in their office.  Most small companies, and individuals use a web hosting service, where your web site, actually runs on their servers at that location.  I'll call the first type a local web server, and the second a hosted web server.


The third type is a free web site like Knol, or sites.google.com or PBWiki.  Most free web sites do not allow the use of PHP at all. So if you're not paying, in some way for your site, the chances are good that you cannot use PHP on it.  If anyone knows of a free web site, which allows the use of PHP, please let me know, by mailing me at fft2001@aol.com


Getting started in PHP is ridiculously simple, if you have already learned any other programming language, you can pick up the basics of PHP quickly.  You will already need to know how to use FTP, to transfer files back and forth to your remote web server.  Of course if you have a local web server, you can simply edit the files in place.  Some FTP programs also allow you to edit files directly on the server, for example I use FireFTP which is integrated into my Firefox browser as an add-on (you have to add it), and it allows this.  I also edit my PHP scripts simply using Notepad.  Easy.


Okay let's get started on creating our "Hello, World" script.
Edit a new file in Notepad and name it MyFirstPHP.php
Here is what you're going to put into that file

<?php
echo "Hello World"
?>

That's it!  Pretty easy isn't it?  Save the file, and FTP it to the httpdocs file on your remote web server, or just store it in the httpdocs file on your local server (if you have one).

Now, let's say your website is called http://www.joesboats.com

So, browse to the URL
http://www.joesboats.com/MyFirstPHP.php
Notice that we do not include httpdocs in the URL

(make sure you use this upper and lower case as we named it, otherwise you'll get a 404 error!)


What you should see in your browser is:
Hello World

If this doesn't work, try putting the HTML tags around it like this
<html><body>
<?php
echo "Hello World";
?>
</body></html>


Now you can get a job as a PHP expert!  Just kidding.
You've passed Lesson 1 !


One thing I like about PHP, is that the remote web users cannot actually see your PHP code.  When a web user browses to a page with PHP on it, the PHP commands are preprocessed by the remote server and only the result is cast up for the user to view.  No code is viewable by them.  This is true even if you use a command like fopen() to try to open the file and read it.




--> Forward to PHP: Lesson 2 Variables and Assignment

Comments

Good job

See also this knol in italian
http://knol.google.com/k/e-php-step-by-step#

Sergio Casiraghi - 18 Feb 2010

Yes, in italian: "Grazie!"

Sergio Casiraghi - 18 Feb 2010

Thanks Sergio! Or in Italian how do you say Thanks? Something like "Gratsie!"

Anonymous - 17 Feb 2010

Interesting

Hope you will continue the series.

Narayana Rao - 13 Feb 2010