A Crash course on web development with PHP for beginners (Day 1)
Hi there! This tutorial is meant for beginners who are interested in writing web applications in PHP, so if all you can do with PHP is write a “Hello World” program, then this tutorial is for you. For those of you who are already PHP experts this may seem too ridiculous for you!
If you have already been coding in C-like languages (Java, C ) but not PHP you might want to stick around you may learn a few things.
This tutorial assumes you know your way around a computer. If that’s settled then let’s get started.
A few things you need to know
Variable: a storage location on the computer. It’s simply what holds information for your program.
Compiler/Interpreter: a computer program that transforms source code (what you’ll be writing) into another computer language. Though there are differences between them but we will not go into them.
Server: in this case is a computer where the PHP files are located and will be executed before serving the browser. For example yahoo.com is a server that processes requests from client browser.
Client: a computer that sends requests to the server.
PHP uses an interpreter.
A few more things you need
- Word Processor –Notepad, Notepad etc OR
- Integrated development environment (IDE) – Dreamweaver, Netbeans, Eclipse.
- Interpreter and Server
- Web browser
For the purpose of this tutorial, I recommend a word processor Notepad (IDEs are for advanced development) and we’ll use WampServer as our interpreter and server. In this case, your computer is the server and the client.
Let’s get started
Test your WampServer installation: type http://localhost on your browser. If you get a screen like the one below then you’re ready to go.
Click on the WampServer icon on the task bar and open the www directory.
This is where the folders containing your website will be located.
- Create a new folder and name it sacc01.
- Open the sacc01 folder and create a file index.php
You might need to show your extensions first. To do this:
Windows 7: from the sacc01 window click organize > folder and search options > view
Windows XP: from the sacc01 window click tools > folder options > view
Uncheck the “Hide extensions for known file types” and apply.
Now you will get to see .txt for text files, .doc(x) for word file and .php for PHP files.
Go to http://localhost/sacc01 on your browser. Blank! Isn’t it? That’s because we are yet to write anything. The index.php file is always the first file to be accessed in the folder.
For the period of this tutorial, all our code will be written in the index.php file.
Since this is a crash course, there will be very little explanation. Ok, so let’s test some code!
Start notepad and open the index.php file. Type in the following:
You probably must have figured out that creating a variable is as simple as putting a “$” sign before the variable name.
<?php // this is a comment /* This is a multi line comment */ echo "Hi there. "; print "How are you doing"; echo "<br />"; // this creates a new line with html <br /> tag $heavyweight = "Lennox Lewis"; // this is a variable $lightweight = "Floyd Mayweather"; // another variable echo $heavyweight, " and ", $lightweight, " are great fighters."; echo "<br />"; // this creates a new line with html <br /> tag // Lets do some maths $x = 1; $y = 3; $sum = $x $y; echo 'The sum of '.$x.' and '.$y.' = '.$sum; ?>
Let’s chew on that for a while.