Вы находитесь на странице: 1из 6

What is PHP? A brief introduction to PHP, tutorials, examples.

Simple PHP mail


script.

PHP
HomeBuildingPromotionIncomeAdvancedToolsResources

What is PHP?
PHP, which stands for "Hypertext Preprocessor", is a server-side, HTML
embedded scripting language used to create dynamic Web pages. Much of its syntax
is borrowed from C, Java and Perl with some unique features thrown in. The goal of
the language is to allow Web developers to write dynamically generated pages
quickly.
In an HTML page, PHP code is enclosed within special PHP tags. When a visitor opens
the page, the server processes the PHP code and then sends the output (not the PHP
code itself) to the visitor's browser. It means that, unlike JavaScript, you don't have
to worry that someone can steal your PHP script.
PHP offers excellent connectivity to many databases including MySQL, Informix,
Oracle, Sybase, Solid, PostgreSQL, and Generic ODBC. The popular PHP-MySQL
combination (both are open-source products) is available on almost every UNIX host.
Being web-oriented, PHP also contains all the functions to do things on the Internet -
connecting to remote servers, checking email via POP3 or IMAP, url encoding, setting
cookies, redirecting, etc.

What do PHP code look like?


PHP is a rather simple language. Much of its syntax is borrowed from C except for
dealing with the types of variables. You don't need to think of the types of variables
at all - you just work with their values, not their types. And you don't have to declare
variables before you use them.
Basic Syntax

• File name:
You should save your file with the extension .php (earlier versions used the
extensions .php3 and .phtml).
• Comments:
// This comment extends to the end of the line.
/* This is
a multi-line
comment */
• Escaping from HTML:
A PHP code block starts with "<?php" and ends with "?>". A PHP code block
can be placed anywhere in the HTML document.
• Instruction separation:
Each separate instruction must end with a semicolon. The PHP closing tag (?
>) also implies the end of the instruction.

Here's a small PHP example...


<html>
<head><title>Example</title></head>
<body>
<h1><?php echo "Hello World"; ?></h1>
<?php
$txt = "This is my first PHP script";
/* This line creates the variable $txt and gives it the initial value. Variables in PHP
are represented by a dollar sign followed by the name of the variable. The variable
name is case-sensitive. */

echo $txt;
?>
</body>
</html>

PHP tutorials
Webmonkey's intro to PHP
This intro tutorial will get you from zero to PHP in no time flat.
PHP Manual
The comprehensive PHP manual from its control center PHP.net.
PHP and MySQL for Dynamic Web Sites: Visual QuickPro
Guide (2nd Edition)
by Larry Ullman
This is a good book for people who already have some
programming background but are just starting out with PHP. The
book is an excellent introduction to not only PHP but also to
MySQL and database designing.
For more books on PHP see Resources: Books on Web Designing.

PHP editor
Zend Studio
A powerful, integrated platform for writing and maintaining PHP applications. It
combines a comprehensive PHP editor with advanced code completion engine and an
integrated debugger to speed up development time of PHP applications. Available for
Windows, Linux and Mac.

Simple PHP mail script


This script is not only educational, but also applicable for practical Web development.
It allows you to place a simple form for sending emails on any HTML page. The script
shows you how to gather user input, perform form validation with PHP, and send an
email.
First, make the form page mail.html (you may call it whatever you like)...
<html>
<head><title>Mail sender</title></head>
<body>
<form action="mail.php" method="POST">
<b>Email</b><br>
<input type="text" name="email" size=40>
<p><b>Subject</b><br>
<input type="text" name="subject" size=40>
<p><b>Message</b><br>
<textarea cols=40 rows=10 name="message"></textarea>
<p><input type="submit" value=" Send ">
</form>
</body>
</html>
The form contains the necessary text fields Email, Subject, Message, and the Send
button. The line
<form action="mail.php" method="POST">
tells the browser which PHP file will process the form and what method to use for
sending data.
When the user fills in the form and hits the Send button, the mail.php file is called...
<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php

/* All form fields are automatically passed to the PHP script through the array
$HTTP_POST_VARS. */
$email = $HTTP_POST_VARS['email'];
$subject = $HTTP_POST_VARS['subject'];
$message = $HTTP_POST_VARS['message'];

/* PHP form validation: the script checks that the Email field contains a valid email
address and the Subject field isn't empty. preg_match performs a regular expression
match. It's a very powerful PHP function to validate form fields and other strings -
see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
echo "<h4>No subject</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}

/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent,
or the error string otherwise. */
elseif (mail($email,$subject,$message)) {
echo "<h4>Thank you for sending email</h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>
As you see, the script is simply one if ... elseif ... else statement. At first, it validates
the required form fields. Note that PHP form validation is performed on the server,
after sending all the data. Therefore, it would be a good idea to combine server-side
form validation with PHP and client-side form validation with JavaScript in order to
avoid unnecessary data sending.
If the email address is valid and subject isn't empty, the script sends the mail and
displays the corresponding message. Note how the variable $email is included into
the output string.
You can also use this script to implement the safe "Contact Us" function on your
website. Your visitors will be able to send you a message, but your email address
won't be displayed on the page and spam bots, that parse pages looking for potential
email addresses, won't get it.
Just remove the Email text field from the form and replace the first line of the script
with something like...
$email = 'YourAddr@YourMail.com';
And, of course, you don't need to validate the email address in this case.

• Overview
• Cascading Style Sheets (CSS)
• HTML Frames
• JavaScript
• PHP
• CGI Scripts
• .htaccess file

What's New
Inexpensive Web Hosting
How to choose a fast and reliable service from the bulk of cheap hosting solutions.

Web site builders


Easy way to build a professional looking site for commercial use or just for fun.

Multiple domain Web hosting


A low-cost solution for owners of multiple Web sites.
Related Sites
Home | Site Map | Articles | Cheap Website Hosting | Free Website Builders | Business
Website Builders | Web Page Templates
Copyright © 2002-2008 BuildWebSite4u.com
Home Page
One-page Guide
Site Map
Glossary
Articles
Free Downloads
Contact Us
Overview
»Site Content
»Search Engine Optimization
»Web Design
»HTML Codes
»Web Graphics
»Domain Names
»Web Hosting
»Website Builders
Overview
»Search Engines
Web Directories
PPC Advertising
Optin Email List
Overview
Affiliate Programs
Google AdSense
Selling on the Internet
»Free Merchant Accounts
Overview
Cascading Style Sheets (CSS)
HTML Frames
»JavaScript
PHP
CGI Scripts
.htaccess file
Overview
Free Website Submission
Advanced Web Search
»Site Search Engine
»Secure HTML
Overview
HTML Tutorials
HTML Editors
Graphics Editors
Web Site Builders
»Web Page Templates
Inexpensive Web Hosting
Books on Web Designing
Web Developer News
Related Sites
Optimal Niche
Choosing Keywords
Analyzing Competitors
Site Structure
Body Copy
Meta Tags
Controlling Web Crawlers
Latest SEO News
Web Page Design
Web Page Backgrounds
Web Fonts
Web Site Navigation
Basic HTML Tags
HTML Validators
Web Graphics Formats
Web Graphics Tips
Free Web Banners
Domain Registration
Free Website Hosting
Multiple Domain Hosting
FTP Software
Free Website Builders
Business Website Builders
Search Engine Submissions
Collection of Free Search Engines
Website Link Popularity
Free Credit Card Processing
JavaScript Examples
Site Search Help
Online Service
Flash Website Templates
Corporate Identity and Logo Design
PHP stands for PHP: Hypertext Preprocessor, with that PHP standing for Personal
HomePage [Tools]. This type of acronym is known as a retronym. Originally, in 1994,
the language was designed as a small set of binaries used to collect some basic site
traffic data. In 1997 the parser was rewritten by two Israelis and the name was changed to
the current acronym — it being determined that hypertext preprocessor was a decidedly
more acceptable name in the business world.
PHP is an open-source language, used primarily for dynamic web content and server-side
applications. It is often pointed to as the main competitor with:

• Microsoft's C# - Visual Basic.NET - ASP family,


• Sun's Java - JSP
• Macromedia's ColdFusion
• CGI - Perl

PHP has many open-source libraries included with the core build, and many more are
readily available. Extensions exist to help PHP interface with a number of systems,
including IRC, a number of compression formats, and Windows API. Other extensions
exist to let PHP generate file formats on-the-fly, such as a popular extension which
allows PHP to create Macromedia Flash movies.
Since version 3, PHP has integrated object oriented features. Version 5 built substantially
on this limited functionality, and PHP now has robust object oriented capabilities,
including interfaces, exceptions, destructions, and abstracts.
PHP reached wide-spread popularity with version 4, released in 2000. In 2004 PHP 5 was
debuted, and it is now considered one the top languages used for server-side scripting.
No doubt much of its popularity is due to its relative ease to learn, and its notorious
looseness. Arrays and variables in PHP are able to hold any type of object, variables need
not be declared, and the syntax is remarkably simple.
Unlike many languages, such as C# or Perl, which have primarily a following of more
generalist programmers, many PHP programmers know no other language. This
occasionally causes it to be dismissed as a lesser language, but its growing popularity and
the many robust and efficient sites built using it as a structure seem to dispel this myth.
PHP has occasionally been criticized for what are viewed by some as security flaws, in
comparison to languages such as ASP. A lack of easily understandable error messages, a
sometimes overly robust configuration file, and an obviously incomplete set of built-in
functions are also pointed to as areas which could use marked improvement.
Development continues apace, however, and with each successive build, PHP appears to
address more and more of the concerns raised by its open-source community.

Вам также может понравиться