Using PHP for your Average Web Site
Jason Lotito on 2001 March 07
Jason Lotito on 2001 March 07
Okay, so, here we go again. Another "How to Code PHP Article."
NO, this is not another syntax does this tutorial. This tutorial gives you an overview of how to actually use PHP for real! That"s right. You know that
<?php
$name = "Jason";
echo "$name is cool";
?>
.... will get you:
Jason is cool.
But why not just right out "Jason is cool" in straight HTML? Its faster, easier, and less cumbersome on the server. So lets get on to it, and show you what you can do with HTML.
Annoying Titles
If your like me, you create a template, setup a title, like "PHPNewbie Rocks" and then forget about it. Every page is named "PHPNewbie Rocks" and your fine with it. But that can be a problem, especially if someone is trying to bookmark a page, of the search engines come through.
The more descriptive your titles, the better off you will be in the end. So, how do you handle this "lazy factor"?
Well, pretty much every page people create has a headline. For example, this page"s headline is "Using PHP for your Average Web Site." That would be great in the Title of the Page, wouldn"t it? Well, you can easily do this by setting up a variable.
<?php
$headline = "Using PHP for your Average Web Site";
?>
<title>PHPNewbie.net - We Rock! -
<?php
echo "$headline";
?>
</title></head>
<body>
<center><b>
<?php
echo "$headline";
?>
</b></center>As you can see, by typing it in once, we take care of everything that uses it. That way, it saves us time and energy. We have also made the second instance of the variable Bold and we can easily change each instance if we wanted.
Functionize It
If you haven"t discovered the power of PHP functions, shame on you. And better yet, you can create functions yourself. How?
<?php
function link($url,$name) {
echo "<a href="$url" target=_blank>$name</a>";
}
?>
Okay, so this is really easy. the first part is the function command. "function link" tells PHP to create a function called link. Now, the next part, "($url,$name) defines the needed attributes when you call this function. Then, you see "{" and at the end you have "}" Everything between these to bracers will be done when the "link" function is called. So, lets take a look at how to apply this.
<?php
// We set the name of the Headline, like I showed you in Tip 1
$title = "Links";
// This is the function explained above
function link($url,$name) {
echo "<a href="$url" target=_blank>$name</a>";
}
// A bunch of Echo Statements to setup the page format and layout
echo "<html><head>";
echo "<title>PHP Newbie - $title</title>";
echo "</head><body>";
// Hey.. you know what this does!
echo "<h1>$title</h1>
// Setting up some arrays.... I guess I need to do an tutorial on this?
$url[1] = "http://www.phphead.com";
$url[2] = "http://www.phpdeveloper.com";
$url[3] = "http://www.php.net";
$name[1] = "PHPHead";
$name[2] = "PHPDeveloper";
$name[3] = "PHP";
/*
This is the confusing part. We are just using a while loop here.
While $i is less than or equal to 3, we are going to run the link() function and increase $i by 1.
That way, if you look inside the link() function, you will see $url[$i],
we can actually input all the variables.
At first, $i is 1, so the Function looks like this:
link($url[1],$name[1]);
After that, it hits $i++ which increases it by 1 each time, so 1 become 2.
Now $i is equal to 2, and we go back to the top of the which loop and do it over again
until $i is greater than 3.
*/
$i = 1;
while ($i <= 3):
link($url[$i],$name[$i]);
$i++;
endwhile;
?>
Wow, okay, so what does that do!?! It"s very easy. Just take a look at it step by step. So, how is this useful. Well, imagine how easy it would be to recode practically everything you do.
Let"s say you have a table format you use every single time.
<table width=100% border=0 cellpadding=0 cellspacing=0><tr>
<td bgcolor=valign=middle>
<font size=3 color=Table</b></font>
</td>
</tr><tr>
<td bgcolor=content of the table
and you use this table setup all throughout your web site.</font>< BR> </td>< BR></tr></table>
Okay, now, that was a lot to type, and I don"t want to type that over and over and over again. So, how can I make it simple?
Make it a function!!!
Let me show you how.
function tableTop($title) {
echo "<table width=100% border=0 cellpadding=0 cellspacing=0><tr>
<td bgcolor=color=bgcolor=
and then you also need the bottom, which is:
<?php
function tableBottom() {
echo "</font>
</td>
</tr></table>";
}
?>
Okay, so how do you use this? Let me show you,
<?php
tableTop("Hey, this is a table");
?>
This is the content
<?php
tableBottom();
?>
That replaces all the coding you had to do. The benifits are many. Now, you can reuse that same table, over and over again, you can also change all the tables quickly and easily. And, unlike Style Sheets, which are still not completely supported, PHP is!!!! It"s server side, so it makes it really easy to do.
Well, I hope this helps you out somewhat in applying PHP to your every day web site needs.
