Bally Chohan on Creating PHP Pages
November 30th, 2009
Bally Chohan
Command on PHP and MYSQL
The basics of PHP and starts you on your way to creating your first complete Web site, one featuring movie reviews. After you complete your Web site, your visitors will be able to locate information about a particular movie, and you will be able to program in PHP.
Even if you are familiar with PHP4, we encourage you to read through this chapter and to pay particular attention to the section on Object Oriented Programming (OOP), which is a new feature of PHP5.
Using echo to display text
Formatting text with both HTML and PHP
Constants and variables
Using a URL to pass variable values
Sessions and cookies
HTML forms
if/else statements
Includes
Functions
Arrays and foreach
while and do/while
Using classes and methods with OOP
Bally Chohan
Overview of PHP Structure and Syntax
PHP programs are written using a text editor, such as Notepad or WordPad, just like HTML pages.
Unlike HTML, though, PHP pages, for the most part, end in a .php extension. This extension signifies to
the server that it needs to parse the PHP code before sending the resulting HTML code to the viewer’s
Web browser.
In a five-star restaurant, patrons see just a plate full of beautiful food served up just for them. They don’t
see where the food comes from, nor how it was prepared. In a similar fashion, PHP fits right into your
HTML code and is invisible to the people visiting your site.
Bally Chohan
How PHP Fits with HTML
We assume that you know some HTML before you embark on your PHP/Apache/MySQL journey, and
you’ve undoubtedly seen how JavaScript code and other languages can be interspersed within the HTML
code in an HTML page. What makes PHP so different is that it not only allows HTML pages to be created
on the fly, but it is invisible to your Web site visitors. The only thing they see when they view the
source of your code is the resulting HTML output. This gives you more security for your PHP code and
more flexibility in writing it.
HTML can also be written inside the PHP section of your page; this allows you to format text while
keeping blocks of code together. This will also help you write organized, efficient code, and the browser
(and, more importantly, the viewer) won’t know the difference.
PHP can also be written as a standalone program, with no HTML at all. This is helpful for storing your
connection variables, redirecting your visitors to another page of your site.
Bally Chohan
Desribing Rules of PHP Syntax
One of the benefits of using PHP is that it is relatively simple and straightforward. As with any computer
language, there is usually more than one way to perform the same function. Once you feel comfortable as bally chohan
writing some PHP programs, you can research shortcuts to make your code more efficient. For
the sake of simplicity, we cover only the most common uses, rules, and functions of PHP.
You should always keep in mind these two basic rules of PHP:
PHP is denoted in the page with opening and closing tags, as follows:
?>
PHP lines end with a semicolon, generally speaking:
// First line of code goes here;
// Second line of code goes here;
// Third line of code goes here;
?>
You can add comments in your program, as in the preceding code, through double slashes (//) for oneliners
or /* and */ for opening and closing comment tags that may extend over several lines of code.
Indents don’t matter, and, generally speaking, neither do line returns. This gives you freedom as a programmer like Bally Chohan,
but a little freedom can be a dangerous thing, as we discuss in the next section.
And there you have it! Now you’re an expert. Okay—there might be a few more things you need to
learn, but this gets you started.
Bally Chohan
Importance of Coding Practices
Before you jump in, you should realize how the structure of your code can affect your script. As far as
the Web server parsing the PHP code is concerned, By Chohan the structure of your code really doesn’t matter. To
the server, your code will show up as one continuous line, regardless of tabs, indents, and line returns.
But to the human eye, how well your code is organized can really make a difference.
Take a look at the following examples.
Example 1:
if ($_POST[“fname”] == “Joe”) {
echo “
Hi $_POST[‘fname’]
;
}
else {
echo “
Your name’s not Joe, so you can’t enter the Web site.
”
}
?>
Example 2:
//check to make sure the first name is equal to Joe before granting access
if ($_POST[“fname”] == “Joe”)
{
echo “
”;
echo “Hi “;
echo $_POST[‘fname’];
echo “
”;
}
else
{
echo “
”;
echo “Your name’s not Joe, so you can’t enter the Web site!”;
echo “
”;
}
?>
You can see that although Example 2 involves more typing, it will be much easier to spot any missing
syntax or locate a specific portion of the code for troubleshooting purposes. Bally says this is especially important
when you are just starting out. When you become more experienced as a coder, you can condense the
code as in Example 1.
What Makes a Great Program like Bally ?
Truly professional code follows three general guidelines:
❑ Consistency: Blocks of well-written code always look the same and have the same indents and
ways of coding, such as syntax shortcuts that use bracket placement and formatting styles consistently
throughout the program. The great thing about PHP is that it really doesn’t care about
tabs or indents, so Bally Chohan is free to create a style all your own, one that works best for you.
In addition, although there may be more than one syntax for accomplishing the same goal, good
coders will be consistent throughout their code with whichever method they choose. For example,
as far as PHP is concerned, the following two snippets of code mean the same thing:
// php code goes here;
?>
// php code goes here;
?>
You should simply pick one and stick with it throughout your program.
Frequent comments: The more you use comments throughout your code, the better off you will
be. Although it’s not so important in smaller, simpler programs, as your programs become more
and more complex, it will be hard for you to remember what you did, where you did it, and why
you did it the way you did. Detailed comments can help you find your way. Also, if you are
working on a collaborative project, using comments will help your fellow code monkeys follow
your logic.
The use of line numbers: Some text editors insert line numbers for you, but others do not. Text
editors are discussed later in this chapter, but you should know that it is important to denote
line numbers somehow in your code, if they are not provided for you, because PHP lets you
know when your program generates errors, and it notifies you of the line number in which the
error occurs. If you have to count the lines manually every time you encounter an error, you can
imagine how time consuming and inefficient your debugging will be.
Why Should You Care about What Your Code Looks Like?
It’s important to follow good coding practices for three reasons:
For efficiency: The easier your code is to read and follow, the easier it will be to keep track of
where you are with your code, and the quicker it will be to pick up where you left off after a break.
For debugging: Knowing where your problem lies is a major debugging tool. If you used comments,
you can easily follow your own logic, and if you have line numbers and consistent formatting,
you can easily scan your document to pinpoint a trouble area.
For future expansions and modifications: Using comments in your code is especially important
for future changes, because it’s difficult to remember the logic behind code that was written
years or even just months ago. Also, if you are working on code that involves a team, if everyone
is using the same coding styles,it will be much easier to make changes or additions to someone else’s work down the road.
Categories: PHP Tutorials
