Free web design training service and resources by the Rochester Web Wizard @ Data Info Magic

Free web design training guide and resources by the Rochester Web Wizard. Sponsored by Data Info Magic.

Lesson 4 : Getting Started Writing PHP Scripts

PHP (recursive acronym for "PHP: Hypertext Preprocessor") is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. PHP is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form data, generate dynamic page content, or send and receive cookies. But PHP can do much more. PHP can be used on all major operating systems, including Linux, many Unix variants (including HP-UX, Solaris and OpenBSD), Microsoft Windows, Mac OS X, RISC OS, and probably others. PHP has also support for most of the web servers today. This includes Apache, Microsoft Internet Information Server, Personal Web Server, Netscape and iPlanet servers, Oreilly Website Pro server, Caudium, Xitami, OmniHTTPd, and many others. For the majority of the servers PHP has a module, for the others supporting the CGI standard, PHP can work as a CGI processor.

To get started with writing PHP scripts, try writing your first PHP script. You really need to strongly consider purchasing a reference book. The best book resource for PHP is the Sams Teach Yourself PHP in 10 Minutes by Chris Newman. It are inexpensive (less than $20) short and consice. It is available almost anywhere books are sold including Amazon and Barnes & Noble online.

 

Reminder: You need to have XAMMP including the Apache Server and PHP installed and running to try PHP locally. Your HostMonster account already has Apache Server and PHP set up and ready to go. For other Hosts you will need to check on their particular procedure and whether they support it. All you need to do is change the file extention to .php and add your PHP code to the HTML. A PHP page can still run HTML code. You just nee to start <? and stop ?> the PHP code.

 

PHP and MySQL are made for each other!

PHP is made to perfectly allow users to interact with PHP on a web page while not even being aware that the are doing any MySQL at all. That is because it is a Server-Side Script that runs on the Host Server and not on the Local Machine like HTML and Java Script. Each part is completely it's own element and runs independently, however. It is good to learn each one alone before trying to combine them. You will be on thought overload and start to lose focus otnerwise. Then when you add in HTML code to the page, you will have three differnt syntax models going on at the same time and it becomes very easy to lose your way. That is the main problem with most other tutorials. They don't separate the skills first and then combine them when you have become comfortable.

 

 

 

PHP is a Programming Language

HTML is not considered a programming language because it doesn't require any action to occur. You are simply styling the look of a page and adding content. People who do this are generally referred to as Web Designers. They are the artists. Sometimes the task is broken down even further and the Designer makes the page in PhotoShop and then hands it off to a technicion, who slices the page into CSS regions and enters the content. Likewise, many Web Designers hire Programmers to add the server side code like PHP and/or MySQL. MySQL is considered a Database Language with a very narrow focus of storing and retreiving information to and from the Remote Server. The farther along you are on the chain, the more money you tend to make because the the increased skills required and the additional time it takes to complete the task.

If you have experience programming in another language like Java, C++, C# or even Visual Basic to some extent, then the code is intuitive. If you have no prior programming experience of any kind then you will really struggle to get down the syntax (rules and strucure for writing statements) and learn the concepts at the same time. Java, C++,C# and PHP use exactly the same syntax for their code. They are all derived from C. Oddly enough, Java Script is not related to Java at all. The inventors just decided to capitilize on the popularity of the name to promote it. Java Script is not a server-side script and it runs on the local machine instead. It is a client-side programming language created by Sun and Netscape. JavaScript can be embedded in HTML pages to create interactive effects and do tasks like validate form data. JavaScript is a separate language from Java. All popular modern browsers support JavaScript. A few hosts support server-side JavaScript. It needs to "take control" of your machine temporarily in order to execute. Some people disable Java Script for that reason. PHP, however offers no such threat.

Disclaimer: At this point you either need some experience or you need to get some more structured training before moving on.

 

 

 

Here are some simple sample scripts

You can get the necessary information to connect from your hosting company. HostMonster lists it right on you CPanel page when you log in. There are also online ftp clients that you may use on that website. Once your local server is set up, I strongly suggest writing, editing and testing on your local server and then uploading all at once.

Please remember, when you make the PHP file you must to put them into the root folder which is C://xampp/htdocs/. Now, test your machine to see if it works for PHP, Create a new file, put the script below and then save it to "index.php" and put it in to your root folder. Run it by typing http://localhost/index.php.

Here is an easy one to start.

<?
echo "Hello PHP";
?>


<html>

<head>
<title>Example #3 Your Very First PHP Script ever!</title>
</head>
<? print(Date("m/j/y")); ?>

<body>
</body>
</html>


<html>
<head>
<title>Background Colors change based on the day of the week</title>
</head>
<?
$today = date("w");
$bgcolor = array(
"#FEF0C5", "#FFFFFF", "#FBFFC4", "#FFE0DD",
"#E6EDFF", "#E9FFE6", "#F0F4F1"
);
?>
<body bgcolor="<?print("$bgcolor[$today]");?>">
<br>This just changes the color of the screen based on the day of the week
</body>
</html>

This is the same application packaged with XAMPP's free Apache Server for PHP/MySQL that you will download later.

 


Getting an average number using PHP

<?
$total_ratings = (3+2+3+1+5+2+3);
$total_votes = 7;
$average = $total_ratings / $total_votes;
print("The Average Rating Is: $average");
?>

 


<html>
<head>
<title>Background Colors change based on the day of the week</title>
</head>
<?
$today = date("l");
print("$today");
if($today == "Sunday")
{
$bgcolor = "#FEF0C5";
}
elseif($today == "Monday")
{
$bgcolor = "#FFFFFF";
}
elseif($today == "Tuesday")
{
$bgcolor = "#FBFFC4";
}
elseif($today == "Wednesday")
{
$bgcolor = "#FFE0DD";
}
elseif($today == "Thursday")
{
$bgcolor = "#E6EDFF";
}
elseif($today == "Friday")
{
$bgcolor = "#E9FFE6";
}
else
{
// Since it is not any of the days above it must be Saturday
$bgcolor = "#F0F4F1";
}
print("<body bgcolor=\"$bgcolor\">\n");
?>
<br>This just changes the color of the screen based on the day of the week
</body>
</html>


You can also find plenty of php at Tizag PHP Tutorial. The PHP website is a great place to learn more about PHP. You'll find an introductory tutorial and a complete online manual.

Now is the time to catch your breath and enjoy your accomplishments! Good luck and have fun.

Looking Ahead: Stick with only the PHP in this lesson. Save the MySQL for the next lesson.

Go on to Lesson 5 (Available Now!)

 
Back to Main Menu