Saturday, June 2, 2012

What is PHP? Introduction about PHP



PHP is a server-side scripting language. This concept is not obvious, especially if you’re used to designing pages with just HTML and JavaScript.

A server-side scripting language is similar to JavaScript in that it allows you to embed little programs (scripts) into the HTML of a Web page. When executed, such scripts allow you to control what appears in
the browser window more flexibly than straight HTML.

The key difference between JavaScript and PHP is simple. JavaScript is interpreted by the Web browser once the Web page that contains the script has been downloaded. Conversely, server-side scripting languages such as PHP are interpreted by the Web server before the page is even sent to the browser. And, once it’s interpreted, the results of the script replace the PHP code in the Web page itself—all the browser sees is a standard HTML file. The script is processed entirely by the server, hence the designation: server-side scripting language.

Let’s look back at the today.php example presented in Chapter 1: File: today.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Today's Date</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>Today's Date (according to this Web server) is
<?php
echo date('l, F dS Y.');
?></p>
</body>
</html>

Most of this is plain HTML; however, the line between <?php and ?> is written in PHP. <?php means “begin PHP code,” and ?> means “end PHP code.” The Web server is asked to interpret everything between these two delimiters, and to convert it to regular HTML code before it sends the Web page to the requesting browser. The browser is presented with something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Today's Date</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>

<p>Today's Date (according to this Web server) is
Sunday, May 16th 2004.</p>
</body>
</html>

44 Order this 350 page hard-copy PHP/MySQL book now!

Getting Started with PHP

Notice that all signs of the PHP code have disappeared. In its place, the output of the script has appeared, and it looks just like standard HTML. This example demonstrates several advantages of server-side scripting:

No browser compatibility issues

PHP scripts are interpreted by the Web server alone, so you don’t have to worry about whether the language you’re using is supported by visitors’ browsers. Access to server-side resources

In the above example, we placed the date, according to the Web server, into the Web page. If we had inserted the date using JavaScript, we would only be able to display the date according to the computer on which the Web browser was running. Now, while this isn’t an especially impressive example of the exploitation of server-side resources, we could just as easily have inserted some other information that would be available only to a script running on the Web server. An example might be information stored in a MySQL database that runs on the Web server computer.

Reduced load on the client JavaScript can slow significantly the display of a Web page on slower computers, as the browser must run the script before it can display the Web page. With server-side scripting, this burden is passed to the Web server machine.

By PHP with No comments

0 comments:

Post a Comment

    • Popular
    • Categories
    • Archives