First PHP Script
Posted in PHP | Posted on 11-11-2009-05-2008
PHP is a server side scripting language. PHP will allow you to accept input from HTML forms. Actually, that’s just the tip of the iceberg. You can do a lot of things with PHP. A few possibilities are: creating a rotating banner, making calculations, adding and extracting data to a database. Again, these are just a few really small examples of what you can do.
In this tutorial, we will be making our first PHP Script. Before we go on, for your script to work, you must have access to a PHP parser. If you have a host server with PHP access, you are good to go. If you do not have a host server, you can download the Apache web server on to your computer. Then install PHP. Apache and PHP are both free. Simply do a Google search on how to install them on to your computer.
Open up your text editor and type out the following:
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>First PHP Script</title>
<meta http-equiv=”content-type” content=”text/html; charset=iso-8859-1″ />
</head>
<body>
<?php
echo “<h1>This is my first PHP script!</h1>\n”;
$name = “John Doe”;
?>
<p>My name is <?php echo $name; ?></p>
</body>
</html>
Save this file as firstscript.php
View the file on your web browser. Here’s my php script. We have nothing fancy here. But let’s take a look at what we have as far as the code goes.
The Code
First we have our regular HTML code. There’s no PHP code here.
<?php
echo “<h1>This is my first PHP script!</h1>\n”;
$name = “John Doe”;
?>
Here is where the first part of our PHP code is. The line with <?php indicates the start of our PHP code. Our next line will echo a line of text. The echo command displays what is in the parenthesis. In this case we will be displaying an <h1> line of text. The /n gives us a new line. To end the echo command, we put a semicolon at the end to indicate the end of this command.
Our next line is very important. We have a variable. This variable is not really important in this script, however the concept of variables is important. Simply put, a variable is a value that can change throughout the life of the script. You’ll see how this works in a minute. In this script, we have a variable called $name. $name is equal to John Doe. Take note of how we declare the variable with the dollar sign. We declare $name’s value with an equal sign and quotations. Again you will notice how we ended the line with a semicolon.
Next we have some lines of HTML code to be displayed. Within that HTML code we have some PHP code .
Within the <p> tags is PHP code. Here we let the parser know that we have PHP code. We then echo (or display) the value of $name (which is John Doe). Then we end the PHP code with a ?>.
I hope you were able to follow along with this example. We worked on a simple PHP script. We even threw in a variable. For practice, try playing around with this script. Change some things around. Change the value of the $name variable. Better yet, try writing your own script. Make something similar to this one.
