Basics of PHP #1

Posted on March 20th, 2008 in Php by admin

You’ll notice we’ve been covering the basics of MySQL and Apache, we will also be covering the basics of PHP. And why under the server section? Databases and scripting languages don’t necessary make a server, but they are integral to the most common stack you find on the web, which is called LAMP. (Linux-Apache-MySQL-PHP).

The first thing we want to do is make sure we have the latest stable apache and php installed.

  • On Debian/Ubuntu: sudo apt-get install apache2 libapache2-mod-php5 php5
  • On Fedora (as root): yum install httpd php
  • On OpenSuse(as root): zypper in apache2 apache2-mod_php5 php5

And just to make sure apache is running: /etc/init.d/apache2 restart

Now as root, we want to be in our document root for apache. (unless otherwise specified by apache. You can check apache’s configuration file “httpd.conf” for the line DocumentRoot to see where the DocumentRoot is on your machine. But typically:

  • On Debian/Ubuntu: /var/www/
  • On Fedora: /var/www/html/
  • On OpenSuse: /srv/www/

for the rest of the tutorial i’m going to assume the document root of Debian, but change as needed.

Create a Simple Script

Create a folder in your document root:

$ mkdir scripts

Move to that directory:

cd scripts

Create a file called simple scripts within that directory

touch simple.php

Use your text editor of choice to edit the file

nano simple.php

and place this text within it:

<?php

print “Welcome to TuxTraining.com”;

?>

Note: For your commands to work, or “execute,” they need a semicolon (;) at the end.

Save the file. Here’s what each line of this PHP script does:

  • <?php

This is the opening PHP tag. PHP code is always written between the opening and closing PHP tags. Before the next line of code is a blank line. You can use blank lines throughout your PHP scripts. Blank lines allow you to group sections of code together, which makes scripts easier to read.

  • print “Welcome to TuxTraining.com”;

This print command tells the Web server to “print” the words between the quotes to the browser window. Remember: for a command string to execute, there must be a semicolon (;) at the end.

  • ?>

This is the closing PHP tag. No more PHP code can be written after this closing tag without another opening PHP tag.

Run a Script from a Web Page

Now lets move up one level back to our document root:

cd ..

and lets create a default file in the document root

nano index.html

and place this text in it

<html>
<head>
<title>Web Page to Test PHP Script</title>
</head>
<body> Click on <a href=”http://localhost/scripts/simple.php”>this link</a> to run your first PHP script. </body>
</html>

Open a Web browser and go to: http://localhost alternatively you can go to http://127.0.0.1

You should see that states: ” Click on this link to run your first PHP script. ” on your new webpage. As you can see apache by default displays the index.html file.

If all has gone well, when you click on the link you will see: “Welcome to TuxTraining.com”

Insert Comments

Lets move back to the script directory. And lets edit the simple.php file.

cd scripts

nano simple.php

and delete the text currently within the file and place this text in instead

php1.PNGExit and Save the file

TIP: If you’re writing a comment in a script and it wraps to the next line, it needs a new # character in front.

Now if you go back to http://localhost in your webbrowser the same html file should appear, and if you click the link it should look identical to the last script. As you can see we entered commented in a number of ways but they do not display on the page itself. (To make sure you’re not getting a cached page you might want to clear your temporary internet files and cookies before loading both pages).

Format Text Output

Now lets make sure we’re still in our scripts directory

pwd

If not move there

cd scripts

And lets create a 2nd php file called together.php

nano together.php

In the file place the text that’s in the following:

php2.PNG

Now move up to your document root

cd ..

and edit your index.html file

nano index.html

and make sure it looks like this

<html>
<head>
<title>Web Page to Test PHP Script</title>
</head>
<body> Click on <a href=”http://localhost/scripts/simple.php”>this link</a> to run your first PHP script. You can check out the HTML and PHP together in this script <a href=”
http://localhost/scripts/together.php”>here</a> </body>
</html>

Now if you open your browser to the http://localhost you will see a new line and a new link. Click on the “here” link and you should see “Welcome Tux TuxTraining.com”

 

 

Post a comment