Introduction to PHP: Part 1

This tutorial will cover the basics of installing PHP, getting it set up and even running a few scripts.

Some prequesists that I assume you have:

  • HTML knowledge (any will do)
  • Basic programming knowledge
  • The will to learn!

If you'd like to learn more about PHP and the history, I'd suggest you to read the Wikipedia page. Anyway, on with the article!


Setting it up

To begin working with PHP, you will need a web host that supports PHP or a local web server. For a local web server, I would suggest XAMPP for Windows or WAMP. For testing and development purposes, a local web server is effecient. Although, I would suggest WAMP for the complete novice at is very easy to install and use.
Once you have that set up, now what?
 

Writing and running a script

Depending on where you install a web server, create a file called test.php in either
C:\xampp\htdocs for XAMPP or C:\wamp\www for WAMP (again, depending on your install).
Open test.php in your favourite editor or IDE (I would suggest Notepad2 or PHP Designer ).
What now? Firstly, when writing PHP code, your script must start with a PHP tag and end with a PHP tag.
For example:

<?
//my code
?>
<?php
//some more code
?>

Lets go on with writing your first script:



<?php
echo "Hello world!";
?>

Congratulations, you just wrote your first PHP script! But how do I access it? Open up your web browser and go to: http://localhost/test.php

The semicolon


As you may have noticed, the line ends with a semicolon (;)

The semicolon represents the end of a statement.

Integration with HTML

Would you like to integrate your PHP code in your HTML pages? Firstly, you must save your HTML file with a .php extension. So index.html becomes index.php. Secondly, include your PHP code in PHP tags, like we discussed before. Take this into account:

<html>
<head>
<title>Test page</title>
</head>
<body>
<?php
echo "Hello World!";
echo "<h2>Test!</h2>";
?>

</body>
</html>

 

 

Variables

Remember in math class when you were learning about algebra and just didn't listen? A variable might be a new idea to some, but try to grasp this section. So, what is a variable? Well it's a...variable! It can be anything, a string (such as Hello world!) or a number (like 04) or something else. Lets consider the following code:
 

<?php
//Here we assign the variables
$my_variable = "Hello world!";
$a_number = 4;
$Another_number = "3.14";

//Comments are used like this
#Or like this, either way is acceptable

//And here we display them
echo $my_variable;
echo $a_number;
echo $Another_number;
?>
Also note that variables are case-sensitive. For example
$my_variable; 

is different from

$My_Variable;

 

Echo

Using the PHP echo means outputting a variable or a string to show the world! But be careful when using quotes in quoties, like this:
 

<?php
//This won't work due to the quotes inside the quotes
echo "<h3 class="test">Hello!</h3>";
//But my using an apostrophe or a backslash, we can get it to work
echo "<h3 class='test'>Hello!</h3>";
?>

Try some sample scripts on your own accord!

This is a very simple introduction to PHP, and I hope it helps those that want to learn PHP.
Well that's it for this tutorial, but stick around for part 2. This tutorial is very to the point, so if I missed out on anything, let me know! Comments, ideas or suggestions are appreciated.