PHP is a scripting language used to create dynamic web pages. Whenever you point your browser at a web address, the browser sends a request to the web server asking for the page. Often, the server simply has an HTML doc**ent stored on a disk somewhere, and it sends this doc**ent. However, if PHP is used, the server will execute the program code and send the result to your web browser. The code can do anything—query files on disk, create images, modify databases or bring in data from other websites.
If you've never programmed before, PHP is a good way to start. The reason is that it's easy to create programs that actually
do something. PHP makes it easy to, for example, process input from a web form and do something with it.
Prerequisites: PHP code outputs HTML; the wh*** point of PHP is that it produces HTML to be read by a web browser. It can even be "woven into" HTML pages. You should at least know that there are opening and closing tags.
You also need a web server that will run PHP code. Many web hosts will provide this service. It's possible to run a web server on your own computer and try out PHP on it, but that's beyond the scope of this article.
PHP itself is an add-on to a web server.
In this article I'm going to teach the basics of PHP. I'm going to go over the simplest knowledge necessary for using PHP in your webpages. Then I'm going to discuss strategies for continuing your PHP learning, along with some appropriate links.
PHP and HTML
A normal HTML page would look something like this.
<html>
<head>
<title>A normal HTML page</title>
</head>
<body>
<p>
Here's some HTML text.
</p>
</body>
</html>
This code resides in a file on the web server. When requested, the server will provide the file to browsers. However, if there is PHP code in the file (and the server is capable of running PHP), the PHP code will be executed.
PHP code is set off by a set of tags: <?php and ?>. Within these tags, PHP code exists. This code can be interspersed in HTML.
<html>
<head>
<title>A normal HTML page</title>
</head>
<body>
<?php
$text_0 = "This is some text. This text will be assigned to the text_0 variable.";
?>
<p>
Here's some HTML text.
</p>
<?php
$text_1 = "This is also PHP code. You can go back and forth between PHP and HTML.";
# Lines starting with a pound sign are comments.
$text_2 = $text_0;
# Note that $text_0 still exists in this fragment of code. It didn't vanish when we left the previous PHP segment.
?>
</body>
</html>
The two examples I've given so far will look exactly the same to the browser. That's because
PHP never outputs anything unless specifically told to. (Of course, if there's an error, the error message will show up in the HTML.) To have the PHP code do something:
<p>
I'm not going to reproduce the entire HTML page every time.
<p>
<?php
echo "<p>\n";
$time_var = time();
echo "The time function returns: $time_var\n";
echo "</p>\n";
?>
The echo statement directs PHP to output something to the webpage. Now you can do calculations and display the results. There are several other important things to note about the fragment above.
A backslash followed by an n, "\n", is a newline character. It's like hitting Enter on your keyboard. If you don't have these, your text will all be put on one line. (In HTML that doesn't matter, but it's important to be aware of what's going on.)
Variables start with $. Variables are like locations where data is stored. Variables can be any type, from text to numbers to arrays of other data. (If you know about Perl, Perl arrays start with @ and not $, but in PHP everything starts with $.)
In double quotes, variables will be replaced by their values. The rules for when variables get expanded can be a little complex. ...
Functions have () after them. The time function returns the number of seconds since January 1, 1970. That might not seem useful, but there are functions to make use of that value, like the date() function. The point of functions is that they are names for sub-programs; the programs do some calculation and
return a value.
Strategies
Start small. You
can go from knowing nothing about PHP to creating an entire blog system...but it will take time. First, make a program that will output some text. Then make a program that will
interpret form input. Then one that will access a database. Take small steps and make sure you understand each step along the way.
Experiment. This is the best way to learn computer programming. With PHP, as long as you're not using functions that access the disk, the worst things that can happen are 1) error messages or 2) infinite loops, which are annoying, but modern web servers will automatically stop them eventually.
Read the PHP manual. There's a wealth of information in a very clear style.
Have fun! The feeling of making a computer do what you tell it to is a rush unlike any other. ;)
You need to be a member of Urgent Evoke to add comments!
Join Urgent Evoke