Fundamentals of PHP - Part 1
July 19th, 2008 | by Justin Daigle |This is the first tutorial in the Fundamentals of PHP series, In this brief article designed for PHP Beginners, I will be going over the fundamentals of PHP. This includes such things as the syntax, basic data output, variables, and briefly go over file ‘inclusions’ as well as best practices.
Part 1 - Beginning PHP Code
The PHP syntax is much like C, Java, and PERL in that way that you use a lot of curly brackets, and semicolons.
When writing PHP code, you must specify to the interpreter that you are writing code for it to interpret, to do this we use the “<?php” to begin a block of PHP code, and “?>” to end.
For example:
<?php //Code will go here ?>
The above example shows a block of PHP code, it demonstrates the procedure for instructing the interpreter to begin parsing code. You will place PHP code where it says “- Code will go here”
Part 2 - Comments in PHP
When writing code, it is important to document all your code. The easiest way to do this, is using PHP Comments. Comments are just text, usually above or below your code, that explains exactly what it does. This is very useful for other coders who may be reading your code, and is very useful for yourself should you need to read it after you’ve written it.
In PHP, we have two options for displaying comments. We can display comments that span a single line, or we can display comments that span multiple lines.
In this example, we use the prefix “//” prior to our comment, this lets PHP know that the rest of that line is a comment, and will not parse it.
<?php // This line of code will not be parsed by /bin/php =] ?>
In this example, we use the prefix “/*” followed by our comments, followed by the suffix “*/”. This lets PHP know that ALL of the code in between the prefix and the suffix, should not be interpreted.
<?php /* This line of code will not be parsed by /bin/php =] Neither will this one Or this one */ ?>
Part 3 - The hello word
If you’ve ever taken a programming tutorial before, you will be familiar with the classic “Hello world!”, these two words are commonly used to demonstrate basic text output.
Before we go ahead and write our Hello World, let’s talk about the PHP syntax. When writing a line of PHP code, you must end it with a semicolon. Much like when you are writing a sentence, you end it with a period. Essentially the line of code is our sentence, and the semicolon is our period.
Now we are ready to discuss the actual Hello World. PHP gives you two choices for basic text out, these two choices are the print($string) and echo($string) functions. ($string represents a variable containing code - we will get to that later) So for this tutorial we will be using both.
<?php
//Output "hello world" using the echo function
echo("hello world");
//Output "hello world" using the IDENTICAL print function
print("hello world");
?>
Part 4 - Variables
Variables in PHP, are used to store data that ‘varies‘ in type and contents. For example you can have a variable that is an integer, a string, or an array. The variable contents can contain anything you please.
Variables are very easy to setup and work with, unlike other programming languages you do not need to declare them. This is very handy and makes for quick development. Variables always contain a dollar-sign as a prefix. “$name” is an example of a variable.
The following code will set a value to a variable, and then output the variables value using the echo() function.
<?php //Set value to variable $string = "Google is a nice search engine =]"; //Echo the string echo($string); ?>
While that is a very basic example of a variable, it should give you a general understanding of how they work. One thing you should remember is that when you are setting a variable’s value to an integer, you don’t need the quotations around the text.
Part 5 - File inclusions
Before I start talking about what you can do with inclusions, I might as well tell you what exactly an inclusion is. Basically when you “include” a file, you are fetching it and parsing it. Let’s say Bob has “file1.php” and “file2.php”, file1.php is his main code, and file2.php contains some functions he wants to use, Bob needs a way of essentially importing the code from file2 into file1, bob would use an include statement to make this happen.
This code example assumes that you have a file named “included.php” that contains valid php code. To simply make this file, paste the “hello world” code into “included.php“, this way you can see how an include works.
This example will include the code from the file “included.php“, PHP will automatically parse this code:
<?php
//File inclusions
include("included.php");
?>
If you setup the “included.php” file to contain the Hello World code from Part 3, then when you run the file you should see the output “Hello World”
I hope that this article has given you a basic understanding of beginner PHP (and programming) concepts. Since this tutorial is getting incredibly long, I have decided I will branch out my beginner tutorials into a series.
The next tutorials will cover such subjects as:
- Loops
- Arrays
- Working with files
- Parsing data
- Forms and their associated variables
I hope you enjoyed this tutorial, please leave a comment.

You must be logged in to post a comment.