Page 4
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:
<html>
<body>
<?php
print ("I am the CHICKEN MAN");
?>
</body>
</html>
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 Web 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, you find this
by looking at the Network settings in your Control Panel. Under the "Identification"
tab you'll see "Computer name." 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 on the Net, 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:
Parse error: parse error in C:\Inetpub\wwwroot\webmonkey_article\test9.php
on line 12
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 HomeSite 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?
Keep on Truckin'
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:
<html>
<body>
This text right here (or any HTML I want to write) will show up just before
the PHP code stuff.
<p><p>
<?php
// first, this $PHP_SELF thang is
// an environment variable that'll show the
// path from your root folder to this
// document itself, like /webmonkey_article/test3.php.
// I put this in just for fun.
// NOTE: This may only work if your server
is Apache.
print "$PHP_SELF";
// next we have to "print" any
// HTML code we want the browser
// to follow to determine
// the layout of the results page.
// In this case, we're adding a <p> tag
// the <p> tags could have been put
// inside the same print statement as the
// "I am the CHICKEN MAN" text.
// between the $PHP_SELF text and the
// next bunch of stuff.
print ("<p>");
print ("I am the CHICKEN MAN");
print ("<p>");
/* This next "phpinfo" business outputs a long page that
tells you exactly how your version of PHP is configured. This can be useful
when troubleshooting problems down the road */
phpinfo();
?>
</body>
</html>
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.
next page»
|