PHP Tutorial for Beginners
PHP is a powerful scripting language that fits
gracefully into HTML and puts the tools for creating dynamic websites in
the hands of the people — even people like me who were too lazy to
learn Perl scripting and other complicated backend hoodoo.
This tutorial is for the person who understands HTML but doesn’t
know much about PHP. One of PHP’s greatest attributes is that it’s a
freely distributed open-source language, so there’s all kinds of
excellent reference material about it out there, which means that once
you understand the basics, it’s easy to find the materials that you need
to push your skills.
Introduction
What Is PHP?
So, what is this whole PHP business all about?
PHP is a program that gets installed on top of your web server software. It works with versions of Apache (
Tutorial:Apache for Beginners), Microsoft IIS and other server software packages.
You use PHP by inserting PHP code inside the HTML that makes up
your website. When a client (anybody on the web) visits a web page that
contains this code, your server executes it. That’s why you need to
install your own server in order to test PHP locally — the server is the
brain here, not your browser. Users don’t need any special plug-ins or
anything to see your PHP in action — it gets to the end user as regular
old-fashioned HTML.
PHP is a scripting language, like HTML. That means that code does
not need to be compiled before it gets used — it gets processed on the
fly as necessary.
Before we dig in, you should know about a site called
PHP.net.
PHP is an open-source language, and PHP.net is its control center, with
extensive reference material about the language and tips sent in by
users across the globe. PHP.net has exceptional, deep information about
the language, but it can be a little cryptic for the newcomer. We’ll
look more closely at how to use PHP.net at the end of this tutorial.
So, what kinds of things can PHP do? Welllll … it can:
- take info from web-based forms and use it in a million ways
(store it in a database, create conditional pages depending on what the
forms said, set cookies for later, send e-mail, write your mom on her
birthday);
- authenticate and track users;
- run threaded discussions on your site;
- serve different pages to people using different browsers or devices;
- publish an entire website using just a single layout template (server-side includes-style);
But before we can get to the specific uses of PHP, we need to start
with a quick preview of the building blocks of PHP, beginning with a
sample script. This example script is titled “chickenman.php.” When
called by a web browser, it would simply read, “I am the CHICKEN MAN!”
5 | print ( "I am the CHICKEN MAN" ); |
The
?php and
?¢ tags start and end a PHP script,
and your meat goes in the middle. Got that? Good! Now let’s walk through
the basic rules you need to know to before you can write your first PHP
script.
What you’ll need
Before we begin, you will need to install a server on your own
machine in order to test your PHP scripts locally. You can install
WampServer for Windows machines from
http://www.wampserver.com/en/ In order to have a Localhost machine. If you’re using a Mac you can get MAMP from
http://www.mamp.info.
If you have space on a web server which supports PHP, you can
also test your PHP there, but this is kind of a pain because it means
you’ll need to FTP your files or telnet in every time you want to change
something.
Steps
The Basics
The code itself fits right inside a page’s HTML, and like HTML it is
made up of plain ol’ text. So a page that displays the words “I am the
CHICKEN MAN!” message would sit inside an HTML page named
something.php, like this:
03 | <title> Chicken Man Example </title> |
06 | <font color= "red" >My PHP code makes this page say:</font> |
09 | print ( "I am the CHICKEN MAN" ); |
See how that works? The HTML is rendered as regular HTML, but everything inside the
?php and
?¢ tags gets processed as PHP.
Basic Syntax
It’s time to write your own first PHP script. The basic rules of PHP are as follows:
Naming Files
In order to get a PHP script working, the file it’s in or the file that it calls to do the heavy lifting must end in
.php (earlier versions used the file extensions .php3 and .phtml). Like HTML, your files are saved as plain text.
Comments
It’s important to get in the habit of leaving notes about your code with
the comment tags so that months down the road you can make sense of
what you were trying to make your script do. The way you set comments
apart from your code (that you don’t want displayed or executed) is with
either “//” at the beginning of each line, or surrounded by “/*” and
“*/” if you want to comment out several lines:
07 | print ( "I am the CHICKEN MAN" ); |
Code Syntax
Start of Code
Every piece of PHP code begins with “<?php” (or the abbreviated “<?” if your server is configured to handle that).
End of Code
The way to signify that the PHP code is finished is by adding “?>” at the end.
Every Chunk
With a few exceptions, each separate instruction that you write will end with a semicolon.
Parentheses
The typical function looks like this …
… where “print” is the function and the stuff that the function works
on sits inside the parentheses, with a semicolon to finish it off.
(Just to confuse you, “print” is the exception that also works without
parentheses.) By the way, echo () is the same as print ().
Much like HTML, the actual formatting of your PHP code (where you
put spaces, line breaks, etc.) will not affect the outcome except those
parts of the code that tell a web browser how to display your page. So
this piece of code …
05 | print ( "I am the CHICKEN MAN" ); |
13 | ... is effectively identical to: |
17 | <?php print ( "I am the CHICKEN MAN" ); ?> |
Like more complicated HTML, it behooves you to use white space and tabs in your code to make the code more understandable.
Ready to write your first script? Let’s go.
Your First Script
OK, so write your first script already! Copy the following script,
but put whatever you want inside the quotation marks. “Print” here means
print to the screen of a web browser when you open the file:
07 | print ( "I am the CHICKEN MAN" ); |
Save the file with any name that has no spaces and ends in .php, and
if you’ve installed a server on your own machine, you need to save the
script somewhere inside the server’s root folder (on Windows this is
typically in the “wwwroot” directory inside the “inetpub” directory on
your C: drive).
The next step is to open the file in your browser. Since you need
the server to run your PHP code, you have to open the file through a
URL that finds the correct file through your web server. On Windows,
your computer name is your root URL. My computer name is “rocketboy,” so
to see the contents of my root directory, I type “http://rocketboy”
into the Web browser and voila! I see the contents of my root folder. To
open the file “chickenman.php” in a directory called “tests” inside the
root directory, I’d type “http://rocketboy/tests/chickenman.php” and
see my example.
If you’re testing on a PHP-able web server, FTP your files
anywhere on your server and they should work when you open them through
the URL.
Go on now and get your first script working. Then come back and
we’ll have some fun. Together. (If you can’t get your first script
working, look at our
First Script Troubleshooting Guide.)
Error Messages
Fun, eh? Fun if it worked. If not — if you had an error in your
script — you probably got an error message that looked something like
this:
3 | parse error in C:Inetpubwwwrootwebmonkey_articletest9.php |
Error messages can be very useful and you’re bound to run into lots
of them. You’ll get a message like this for every line in your script
that has an error. For our purposes, all we really need to know is that
there is something wrong with our code in line 12 of the document
“test9.php,” so let’s look at that line and see if we can figure it out
(good text editors like BBEdit have a function that lets you jump to any
particular line). I always start by looking to see if my basic syntax
is correct: did I leave out the closing tag, a line’s semicolon,
quotation marks?
A Few More Statements
Let’s continue by adding to your test code from the last page to show a couple useful tools.
In the same code that you wrote before, drop in a couple more
statements. As you see, you can gang up more than one PHP function
inside the same opening and closing tags. My comments in the code
explain what each part does:
05 | This text right here (or any HTML I want to write) |
07 | will show up just before the PHP code stuff. |
49 | print ( "I am the CHICKEN MAN" ); |
NOTE: Phpinfo will output a long page of info about your version of
PHP. You don’t need to understand what it all means, I just wanted to
show you that it’s there if you ever need it.
Parham PHP LessonsLesson 01 - Installing a Server
Lesson 02 - PHP Basics
Lesson 03 - Variable/array/function definitions
Lesson 04 - Setting and printing variables
Lesson 05 - Arrays
Lesson 06 - Operators
Lesson 07 - Control structure, if statement
Lesson 08 - Control structure, foreach loop
Lesson 09 - Control structure, while loop
Lesson 10 - Basics of user input
Lesson 11 - Using user input
Lesson 12 - Using functions
Lesson 13 - Creating custom functions
Lesson 14 - Variable scope and global declaration
Lesson 15 - Variable referencing
Lesson 16 - More function examples
Lesson 17 - Regular Expressions
Lesson 18 - Filesystem functions
Lesson 19 - String functions
No comments:
Post a Comment