Introduction to PHP

Nov 7th, 2000 | By

Print this article


Introduction to PHP


Date
: 11/7/00 – 02:41:07 AM

Author
:

Category
: Programming


Page 1 : Intro

Table of Contents

Introduction

The Basics

Variables

Operators

Input

Conclusion

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.

The Basics

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
print
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
echo
and
printf()
. I mainly use
echo
myself, unless I'm putting HTML code in there, in which case I use the
print
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…


Hello World

I mentioned in the previous paragraph that I use HTML code in my PHP. You've got to put the HTML in quotation marks ALL the time when doing this, otherwise, it'll see it as a syntax error, just like with Javascript. Also, you can't use double quotes in an HTML statement like you could before, as now you are already in a pair of double quotes. You'll need to get used to apostrophe's from now on. Here's an example of some HTML in PHP.


print ‘‘;

print ‘
‘;

print ‘

This is HTML code in the middle of a PHP script!

‘;

print ‘
‘;

?>

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
print ''
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.

Variables


$num = 0;

echo $num;

?>

It's pretty simple stuff really. You've got your
tag, and something new here is the
$num
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;

?>

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
$num
at the beginning of the code, and then outputted it. After that line,
$num
takes the value that is in it, which is
0
, and adds 10 to that. Since whatever is on the right side of the equal sign is done first, the value
10
is given to
$num
, and the values
$num
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.


010

The code will now be like this, however..


0

10

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;

?>

Notice how I write out the string, just like I wrote it above in the
Hello World
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…

Operators

+
– 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’;


Input

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

's. Here's an example.


print '
‘;

print ‘‘;

print ‘

print ‘

print ‘

‘;

echo $str;
?>

Just copy and paste that, and give it a whirl. When you type in something, it'll print it out on the page below. Notice the
name='str'
there. And then below, notice how I say
echo $str;
. When you give a name to the values in the form, they become PHP variables. Another way to output the variable, or use it would be to use $HTTP_GET_VARS['str'];. This is if you're using a GET form, and not a POST form. If you are using POST, just exchange GET with POST in there. By the way, that $HTTP_GET_VARS['str'] is an array, which we'll get to in another article. You can use this with any type of form input there is. Play with it a little and see what you can get done.

Since this has been an introduction to PHP, we're not going to continue with anything more for now. If you feel like skipping ahead, and reading some great PHP online documentation, php.net carries that, and I go there all the time. Unless you have experience though, it might be a little difficult to get through. It sometimes doesn't explain things too well, and you're left with the user submissions to try and figure it out.

Conclusion

I'm not much for conclusions, so let me just say this was definitely a learning experience for me to write. It was kind of difficult deciding what to write about here, so I just stuck with the basics. I hope I've created a fairly solid foundation on which you can build, because that was my goal. With each successive article, I hope you can become more and more adept at PHP, and your curiousity will build the more you learn. If you've read through the article, you can now do many things which you might not have been able to do before. You can output strings/variables, create and change variables, receive input from users, change that input. Although what you create not be very complex, you are now an author of dynamic webpages! Congratulate yourself on a job well done. If you'd like something for me to leave you with, a project of sorts, I've got a few ideas.

1) Create a webpage, and output a variable onto the top title bar of the browser window.

2) Create a form, and have the user input a string which will be displayed on the browser title window.

3) Get the amount of time in hours and minutes a person is at work. The person goes into work ALWAYS in the morning, and then leaves ALWAYS at night/afternoon. Here's what the output should look like.


Input arrival at work.

800 (means 8:00AM)

Input time left from work.

400 (means 4:00PM)

Time spent at work is 8 hours and 0 minutes.

There are four different values with which you need to make this work.

1) 800 / 400 (8 hours 00 minutes)

2) 820 / 400 (7 hours 40 minutes)

3) 800 / 415 (8 hours 15 minutes)

4) 830 / 445 (8 hours 15 minutes)

5) 1159 / 1201 (0 hours 2 minutes)(bonus)

If you can get all four of those to work, I commend you, if you can get all five, thats even better. Go to it. By the way, you can only do this problem with the operators I told you about above. If you know if/else statements, you can't use those. The only thing you can use is what I've told you about in this article. I'll give the answers sometime in the near future most likely.

Leave Comment

You must be logged in to post a comment.