Introduction to PHPNov 7th, 2000 | By Archive
Introduction to PHP
: 11/7/00 – 02:41:07 AM
Page 1 : Intro
Table of Contents
Welcome to the first of what is going to be a series of tutorials on PHP. Believe it or not, PHP stands for ‘Hypertext PreProcessor’(at least the letters are right). It was designed specifically for use in making homepages. The manual says this: ‘PHP Version 3.0 is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly.’. This guy states it pretty thoroughly here. Not only is PHP an easy to learn language, but once you learn it, it can be a great tool. Despite it saying ‘Version 3.0′ up there in the quote, the latest version is 4.03, but we'll be using 4.02. The differences between 4.02 and 4.03 are pretty small, but the differences between 3.x and 4.x are much more. I won't go into them in this article, but I'm sure you can find out if you go to php.net.
Like it says in the quote up there, PHP is a server-side scripting language, much like Perl/CGI. What this means, is you'll need an actual program to compile the code. Since PHP is free to download, and is compatible with both Linux and Windows, downloading it should not be a problem. If you've never installed PHP before, the first time might be a little daunting if you're downloading the regular installation files. Luckily for us though, some nice guy put together a program that not only installs PHP automatically, but also installs an Apache webserver, as well as mySQL. There is a binary setup file, so you just run the program and everything is installed in minutes. The config files automatically set your IP address to localhost, so when you start up Apache, you just type http:/localhost in the browser, and you're set. You put your PHP files in the htdocs directory. The program is call PHPTriad, and you can find it at http:/sourceforge.net/projects/phptriad/. If you want to go the hard route, then go to http:/php.net/downloads.php and you can pick up the latest binaries to take a crack at. Don't say I didn't warn you if it turns out to be difficult. And one more thing, since PHP is opensource, you can download the source files there too.
If you're wondering to yourself, why should I learn PHP? I may not have the answer to your question. The language definitely works for me. It's got ease of use, portability, great database support, great syntax, speed/agility, and much more. This is a pretty general list here, and is subjective to each person, but it's true for most people.
As I said in my opening sentence, this article is hopefully going to be the first in a series of tutorial articles on PHP. I plan on covering everything from string functions, to mySQL databases. This won't all happen overnight though, it'll most likely span over a little time, but hopefully it'll eventually all get out there. In this first article, I plan on introducing PHP to those with little programming experience. If you already know PHP, you probably shouldn't even waste your time here. If you don't know PHP, but you do know a similar language, take a read and compare. If you have no idea what I've been talking about for the past few paragraphs, maybe you should turn back. Those with a pretty basic understanding of programming and such will probably best benefit from reading this. By the way, I doubt anyone will, but if you find any mistakes in the code, or suggestions, let me know, as I'm always looking for ways to improve myself. On that note, let's get started.
The means of programming I use is EditPad. I find it to be much better than Word/Notepad, and I've been using it for a while now. Anyway, no matter what program you use, once you type in your code, and debug it, save the file with a *.php extension. If you're using PHPTriad, make sure to start up Apache, and copy the file to your htdocs directory. Otherwise, save it to whichever directory is your PHP one. Let's get on with it.
If you've ever programmed anything before, you've most likely had some experience with the ‘Hello World’ program. You know, the program where you output the words ‘Hello World’ to the screen? This is pretty much where everybody starts at one point or another, and this is where we'll start today.
Let's disect this. If you look at it, it isn't complicated at all. The
tag tells the browser the PHP codelet is starting, and the
tag tells it the code is over. You can also use
to do this if you want, but the first two suit me just fine. The
command here tells the screen to print out whatever is to the right of it here in the quotation marks. There are other means of printing something on the screen. Examples are
. I mainly use
myself, unless I'm putting HTML code in there, in which case I use the
command. I have no idea why I do this..just weird I guess. Please notice, after the the quotation marks, there is a semicolon. After just about every line if PHP, there will be a semicolon. Remember this, because it'll save you a lot of time if you ever have a program with an error you can't figure out, and it turns out to be a missing semicolon or something. The output of this should look like this…
This is HTML code in the middle of a PHP script!
Give that a run and see what happens. It's all pretty understandable and easy to remember. You've got the opening
tag, as well as it's closing partner, and in the code you've got the
statements with the HTML in them. When the code runs, it just runs it like regular HTML. Pretty nifty eh?
You can also have PHP inside a .html page. Here's an example of that.
print 'This is a PHP codelet inside an html page. You may have to change the file extension name to .php for it to work.';
Now that you can output information and whatnot, let's get to some more interesting stuff. Variables. Variables in PHP are handled very well. If you're coming from a C/C++ background, PHP's variable system might seem a little strange to you, but to those of you with a clean slate, this will seem great. It's simple and easy to remember. Put a $ in front of everything that is a variable. You don't need to declare(although you can) a variable at all, whether or not it's a string, or a number.
‘That's the beauty of the juice weasel!’
keeps coming back to me for some reason. Here's a little codelet to give you some help on the matter. By the way, I won't be doing html header tags anymore, as they aren't needed, and just add baggage to the code.
$num = 0; echo $num; ?>
$num = 0;
It's pretty simple stuff really. You've got your
tag, and something new here is the
variable definition. There's no need to declare a variable at the beginning of the code now, you can do it anywhere. As you've probably guessed, this code will output the number 0
to the screen. Want to do a little variable changing? Here goes.
$num = 0; echo $num; $num = $num + 10; echo ' echo $num; ?>
$num = 0;
$num = $num + 10;
You'll notice I did this
$num = $num + 10;
thing here. If you don't have any programming experience, you might also be wondering how I can adding a number to itself like that. Or maybe not. In any case, You see that I've declared
at the beginning of the code, and then outputted it. After that line,
takes the value that is in it, which is
, and adds 10 to that. Since whatever is on the right side of the equal sign is done first, the value
is given to
, and the values
is now 10. Also, notice how I put in the
there. This is so the output will go on a newline. If I didn't put it there, the output would just all be on one line, like this.
The code will now be like this, however..
Instead of just having two numbers there, with nothing to claim them as their own, let's append a little text to the beginning, so we can see what is what.
$num = 0; echo 'The number first equals ' . $num . ' $num = $num + 10; echo 'but now it equals ' . $num; ?>
$num = 0;
echo 'The number first equals ' . $num . '
$num = $num + 10;
echo 'but now it equals ' . $num;
Notice how I write out the string, just like I wrote it above in the
example, and then I put a period in between the string and variable. Then I put another string on the other side there. The code above where I used a whole new line just so I could add a
in there seems like a waste now doesn't it? See if you can fix that code so it uses the same appending method as the example directly above.
Play around with what I've shown you for a bit and see what you can come up with. Make all the variables you'd like and do what you want with them. Here are some operators(+/- type stuff) you can use…
– This is the addition sign.
– This is the subtraction sign.
– This is the multiplication sign.
– This is the division sign.
– This is the modulus operator. Modulus is the remainder of a division. For example, 10 % 2 = 0. 10 % 4 = 2.
– Adds 1 to value. For example, $num++;
– Subtracts 1 from values. For example, $num–;
– This is the append operator. For example..
$blah = ‘This old ‘;
$blah .= ‘house’;
There are more operators, but they mostly have to do with looping and condition statements, which is beyond the scope of this article. The last part of the article I want to discuss here is possibly one of the most important parts of the article. Input. What if you want to get a variable from a user? The only way to do it is by using