Web Design Tips and Tricks

PHP Easy Dynamic Websites

PHP: Easy Dynamic Websites.

PHP is the most popular scripting language on the web, and the reason for that is how easy it makes it to create dynamic websites quickly. If you're already a programmer, you'll be able to learn the basics of PHP in about five minutes, and if you're not then it probably won't take much longer.

Getting Started in PHP.

There's a tradition in programming that the first thing you do in any language is say 'Hello World'. Well, here's how you do that in PHP. First of all, create a file in your server's root directory called index.php. Put this text in it:

<?php

echo "Hello World";

?>

Let's look at this bit by bit. The first line means 'what follows is PHP code'. 'echo' is the PHP command to send text to the web browser, and each line of PHP has to end with a semicolon. Finally, the last line means 'end of the PHP code'.

Now, the power of PHP is that those start and end tags can do anywhere in a normal HTML document, as many times as you like. For example:

<html>

<head>

<title>my page - <?php echo date(); ?></title>

</head>

<body>

<?php

$total = 1 + 1;

echo $total;

?>

</body>

</html>

This is a complete HTML document with pieces of embedded PHP. The first PHP section inserts the date into the title, and the second writes the answer to 1 + 1 (that's 2, you know) as the content of the document – the word with a dollar before it is a variable, storing the result of the sum. Where this all becomes extremely useful is that your PHP code can open a connection to a database, read data from it, and then the text into a template, along with other things from the database like the headline, the author's name and the date it was written.

Useful PHP Functions.

Here's a quick reference of the most useful PHP functions to help you get started.

date. This function returns the date in a format you specify using letters. For example, date("D j M Y") outputs dates in this format: Mon 1 Jan 2010.

echo. Writes text to the document. You can use <?= as a useful shortcut for <?php echo.

explode. Divides up some text into an array by looking for 'seperator' letters or characters. Can be good if you're using odd characters like | to separate data somewhere in your program.

fopen. Opens a file on your web server, but can also be used to open a URL and so connect to another server.

fread. Reads the contents of the file, either all at once or line by line.

header. Allows you to set your own HTTP headers – most often used to control which MIME types things are sent with (the content-type header), or to tell the browser whether to cache or not (the cache-control header).

md5. Takes some text and produces a 'hash' using the MD5 algorithm. This is often used to allow checking of users' passwords without needing to save their passwords in a database in plain text. The sha1 function does the same thing, and is more secure but slower.

mysql_connect. Connects to a MySQL server. You have to tell it where the server is (usually localhost), as well as your username and password.

mysql_select_db. Chooses which MySQL database to open on the MySQL server you're connected to.

mysql_query. Sends any SQL commands you want to your MySQL server.

mysql_fetch_assoc. Turns the results of a query sent to a MySQL server into an array, to make it easier to use in your program.

str_replace. Replaces one word with another in some text. This is useful when it comes to inserting the HTML tags between paragraphs, for example.

strtotime. Turns an English-language description of a date and time into a number representing that date and time (technically known as a Unix timestamp). This makes them easier to use with a database, as you can sort from the 'highest' (most recent) to the 'lowest' (longest ago) more easily. You can convert back from timestamps again by using the date function.

If you have trouble remembering the names of the PHP functions (they're quite inconsistent), take a look at http://www.ilovejackdaniels.com/php/php-cheat-sheet/ – this page has a 'cheat sheet' with names of common functions that you can print out and keep.

 

 

 Article index
 
5-Simple-Steps-to-Accepting-Payments
5-Steps-to-Understanding-HTML
10-Easy-Ways-to-Promote-Your-Website
5-Ways-to-Avoid-the-1998-Look
6-Reasons-Why-You-Need-a-Website
7-Ways-to-Make-Your-Web-Forms-Better
Ads-Under-the-Radar-Linking-to-Affiliates
AJAX-Should-You-Believe-the-Hype
All-About-Design-Principles-and-Elements
An-Introduction-to-Paint-Shop-Pro
An-Issue-of-Width-the-Resolution-Problem
A-Question-of-Scroll-Bars
Avoiding-the-Nuts-and-Bolts-Content-Management-Software
Beware-the-Stock-Photographer-Picking-Your-Pictures.
Building-a-Budget-Website
Building-Online-Communities
Clean-Page-Structure-Headings-and-Lists
ColdFusion-Quicker-Scripting-at-a-Price
Column-Designs-with-CSS
Content-is-King
CSS-and-the-End-of-Tables
Cut-to-the-Chase-How-to-Make-Your-Website-Load-Faster
Designing-for-Sales
Designing-for-Search-Engines
Dont-Be-Scared-Its-Only-Code-HTML-for-Beginners
Dreamweaver-The-Professional-Touch
Encryption-and-Security-with-SSL
Finding-a-Good-HTML-Editor
Focus-on-the-User-Task-Oriented-Websites
Fonts-are-More-Important-Than-You-Think
Free-Graphics-Alternatives
FrontPage-Easy-Pages
Hints-All-the-Way
Hiring-Professionals-5-Things-to-Look-For
How-Databases-Work
How-the-Web-Works
How-to-Get-Your-Website-Talked-About-on-Blogs
How-to-Install-and-Configure-a-Forum
How-to-Make-Visitors-Add-You-to-Their-Favorites
How-to-Run-Ads-Without-Driving-Visitors-Crazy
How-to-Set-Up-Your-Hosting-in-5-Minutes-Flat
IIS-and-ASP-Microsofts-Server
Image-Formats-GIF-JPEG-PNG-and-More
Its-a-World-Wide-Web-Going-International
JSP-Java-on-Your-Server
LAMP-The-Most-Popular-Server-System-Ever
Making-Friends-and-Influencing-People-the-Importance-of-Links
Making-Searches-Simple
Offering-Free-Downloads-on-Your-Website
Opening-a-Web-Shop-with-E-Commerce-Software
Perl-Cryptic-Power
Photoshop-a-Graphic-Designers-Dream
PHP-Easy-Dynamic-Websites
Picking-a-Colour-Scheme
Printing-and-Sending-the-Two-Things-Users-Want-to-Do
Putting-Multimedia-to-Good-Use
Python-and-Ruby-the-Newer-Alternatives
Registering-a-Domain-Name
Registering-Your-Users-by-Stealth
RSS-Really-Simple-Syndication
Setting-Up-a-Mailing-List
Setting-up-a-Test-Server-on-Your-Own-Computer
Some-Places-to-Go-For-More-Information
Taking-HTML-Further
Taking-HTML-Further-with-Javascript
Taking-Your-Website-Mobile
Text-Ads-Unobtrusive-Advertising
The-5-Principles-of-Effective-Navigation
The-Art-of-the-Logo
The-Basics-of-Web-Forms
The-Basics-of-Web-Servers
The-Case-Against-Flash
The-Confusing-World-of-Web-Hosting-Making-Your-Decision
The-Evils-of-PDFs
The-Importance-of-Validation
The-Many-Flavours-of-HTML
Theres-More-than-One-Web-Browser
The-Smaller-the-Better-Avoiding-Graphical-Overload
The-Top-10-Biggest-Web-Design-Mistakes
The-Web-Designers-Toolbox
The-Web-is-Not-Paper
Time-for-User-Testing
Titles-and-Headlines-Its-Not-a-Newspaper
Tracking-Your-Visitors
Understanding-Web-Jargon
Uploading-Your-Website-with-FTP
Using-Flash-Sensibly
Using-Quizzes-and-Games-to-Get-Traffic
VBScript-Javascript-Made-Easy
Websites-and-Weblogs-Whats-the-Difference
What-Do-You-Want-Your-Website-to-Do
What-You-See-Isnt-Always-What-You-Get
Which-Database-is-Right-for-You
Why-Doing-It-Yourself-is-Best
Why-Java-Will-Drive-Your-Visitors-Away
Why-Word-is-Bad-for-the-Web
Why-You-Should-Put-Your-Content-in-a-Weblog-Format
Why-You-Should-Stick-to-Design-Conventions
Working-With-Templates
Writing-for-the-Web
Site Map