.
Developer Spot - Web Development Tutorials
arrowDeverloper Spot  Tutorials  PHP  Is PHP Right For Your Website? 
 
Development Tutorials
ASP
CGI & Perl
CSS
HTML
Java
JavaScript
Linux
PHP
XML




More Resources
Web Hosting Articles
Web Development News
PHP Manual
Web Hosting Directory
Budget Web Hosting Linux Web Hosting Small Business Hosting
Windows Web Hosting Reseller Web Hosting Web Hosting Articles

Is PHP Right For Your Website?

By Dan Ball
2003-09-27
Reader Rating: 3 out of 5
Bookmark Print Version
Is PHP Right For Your Website?

As a small prologue to this column, I have to say that I, like most people, am a big fan of the old cliche "good price for a good product". To that end, along with many self-proclaimed computer geeks, I am also a big fan of the Open-Source movement in web development. For those few that have not yet heard of Open-Source, let me elaborate... The Open-Source community is a group of geeks that collaborate on development of products, and, with the release of these products, the source code is also given. This in-turn allows the user to recode and customize the application to their needs or add enhancements and release it back to the public. This method of development has resulted in things like Linux, Apache Web Server, and more recently PHP. Not only that, but it keeps the products advancing due to the seemingly endless development cycle.

Now back to our regularly scheduled column...

There are a whole lot of "p" acronyms going on today in web development today, we have ASP, JSP now there is PHP. All of these are scripting languages that run on the server instead of in the visitors browser. This method has many advantages, by running on the server you avoid browser compatibility problems and you also have the advantage of an easier interface with a database that resides on the server.

ASP stand for "Active Server Pages", a Microsoft product, which is, therefore, only runs at it's best on a Microsoft platform. Some companies have made ASP packages for other server platforms, but they never perform like the true MS implementation.

JSP is "Java Server Pages". I have worked with JSP and it is very nice and it costs nothing to download and install on your server (at the time of this writing). The major drawback I find with JSP however, is that the developer had better know a little Java Syntax before trying anything to sophisticated with it. Anything really interactive will require some serious coding, if not have a Java "Bean" or "Servlet" (both are applications that need installation on the server, and are significantly more complex code than typical web programming) developed for it.

Enter PHP...

"Hypertext Preprocessor" to the true geek, is very cool, easy to learn and use and free to implement server-side scripting language. In all honesty, I have just begun to use PHP in the last couple months while rebuilding my home business site, http://www.dbmasters.net/, and moving it to a new host. I saw this host supported PHP and figured I would give it a try. This was one of the better decisions I have made this year so far:-)

For starters, PHP can run on many different servers, and the PHP engine is a free download. It runs very smoothly under Linux, which is also free and open-source. The point is, that except for your hardware costs, you can set up a PHP server for NOTHING, ZILCH, ZIP, ZERO MONEY!

OK, putting the money issue (or, the no money issue) aside, lets dig into how PHP works.

PHP is scripting that is embedded into standard HTML, to make the PHP code do it's thing, however, filename must end in the .php extension (or, .php3 for using features from version 3 PHP if that is how you configure the server). All the PHP code opens with "" (without the quotes, of course). So the code to include a file in your document would look like this:

<?php include "/home/path/to/filename.php";?>

There, you just got the first taste of PHP...you can now do server-side includes in PHP. The only other advice I can give about using includes is that if they are not in the same directory as the page including them, path the include right from the root of the server, relative paths can make problems, which doesn't really bother me personally because I code everything from the root anyway.

In a previous column I discussed building email forms in PHP, so I won't go into great detail here. If you want to read that it is at http://www.allwebarticles.com/articles/1phpmail.html for your reading pleasure. Suffice it to say for this column, it is the easiest way to build an email form I have ever used.

The real strength of PHP lies in it's database interactivity. The site I built runs on a MySQL database (a free relational database). It was the easiest database connectivity I have ever worked with. This is my database connection:

<?php

$database_login="login-name";

$database_pass="login-password";

$database_host="host-name";

$database_name="database-name";

$Err_Mail = "your email address";

$db=mysql_connect($database_host,$database_login,$database_pass);

mysql_select_db($database_name,$db);

?>

With that (replacing the obvious values with the proper username, password, etc.) I am connected to MySQL, for other databases the "mysql_connect" and "mysql_select_db" are just replaced with database specific codes that can be found at php.net.

Then, inside the above php tags insert:

Function MySQLConnect($errmsg, $msg="")

{

$success= mysql_connect($GLOBALS["database_host"],

$GLOBALS["database_login"],

$GLOBALS["database_pass"]);

if (!$success)

{

mail($GLOBALS["Err_Mail"],

"MySQL Connect Failure: $errmsg", mysql_errno(). ":

".mysql_error(), "From: AutoError");

die();

}

}


Function MySQLQuery($query, $errmsg, $msg="")

{

$success= mysql_db_query($GLOBALS["database_name"], $query);


if (!$success)

{

mail($GLOBALS["Err_Mail"],

"MySQL Query failure: $errmsg", mysql_errno(). ":

".mysql_error(), "From: Error Notification");

echo $msg;

die();

}

return $success;

}




and you have your functions to connect and query, if either fails, it sends an email alert to the email address listed above. Save that completed script as "database_config.php", for example, and you can then include it in the top of every page that you need a database connection in, and your connection is built, and you can edit the configuration of the connection in one file and it will apply itself across the whole site.
To display any results from this connection, you just need to query the database with a standard SQL statement and set the format of the data:

<?php


error_reporting(0);


MySQLConnect("There was an error connecting to MySQL",


"Sorry, this service is temporarely unavailable.");

$result = MySQLQuery("SELECT n_id,UNIX_TIMESTAMP(n_date)

AS temp_date, n_title FROM news WHERE (catagory_id=23)

ORDER BY n_date DESC LIMIT 0,3",

"There was an error executing query",

"Sorry, this service is temporarely unavailable.");

while($row = mysql_fetch_array($result))

{

$n_date=date("D, F d, Y",$row["temp_date"]);


echo "<P class=\"bodysm\">

<a href=\"/news/dbmasters_one.php?n_id="

. $row["n_id"] .

"\">" . $row["n_title"] . "</a><br>$n_date</p>";

}

?>

conditionals and loops are coded just like any scripting language and the HTML of your page is added around the PHP code. Inside the PHP code HTML is printed inside the "echo" statement using the "\" as the escape character like in many other languages:

<?php

if ($Name != "")

{

echo "<p class=\"bodymd\">Your Name is $Name</p>";

}

else

{

echo "<p class=\"bodymd\">What is Your Name?</p>";

}

?>

I have found that being even remotely familiar with other scripting languages gives a big step forward to the person learning PHP. So much of the code will already look familiar and function logically as it would in other languages. If you have, or your developer has, experience with Perl, JavaScript, JSP, ASP or other scripting languages, it might be a good time to step into PHP if you are looking at upgrading or changing your current platform, even without changing platforms, chances are PHP will be able to plug right into you existing server setup.
I have been trying to steer away from the exacts of PHP coding, while at the same time, showing enough to demonstrate how easy it is understand and use PHP on your website. For more information and distribution downloads of PHP for you server, visit http://www.php.net/

Editor's note: The code provided above is for illustration purposes only. The format of this online magazine does not allow precise displaying of programming code, so for detailed code, please contact the author at dbmasters@excite.com.


Article Pages:
Is PHP Right For Your Website?

 Rate this article:   Poor          Excellent 


If you found this article interesting, you may want to read these as well:

» Protecting your PHP and HTML Source Code

» Publishing Newsletters Using PHP & MySQL - 4

» Publishing Newsletters Using PHP & MySQL - 3

» Publishing Newsletter Using PHP & MySQL - 2

» Publishing Newsletters Using PHP & MySQL

» Unix Webserver Crontab Basics



 
Development Tutorials: CGI & Perl - CSS - HTML - Java - JavaScript - Linux - PHP - XML
More Resources: Web Hosting Articles - Web Development News - PHP Manual