How PHP Works

Lesson 19 provided a brief introduction to PHP. You learned that PHP is a language that enables you to embed code processed by the server in your web pages. Normally, when a user submits a request to the server for a web page, the server reads the HTML file and sends its contents back in response. If the request is for a PHP file and the server has a PHP interpreter installed, then the server looks for PHP code in the document, executes it, and includes the output of that code in the page in place of the PHP code. Here's a simple example:

<html> <head><title>A PHP Page</title</head> <body> <?php echo "Hello world!"; ?> </body> </html>

If this page is requested from a web server that supports PHP, the HTML sent to the browser will look like this:

<html> <head><title>A PHP Page</title</head> <body> Hello world! </body> </html>

When the user requests the page, the web server determines that it is a PHP page rather than a regular HTML page. If a web server supports PHP, it usually treats any files with the extension .php as PHP pages. Assuming this page is called something like hello.php, when the web server receives the request, it scans the page looking for PHP code and then runs any code it finds. As you learned in Lesson 19, PHP code is set apart from the rest of a page by PHP tags, which look like this:

<?php your code here ?>

Whenever the server finds those tags, it treats whatever is within them as PHP code. That's not so different from the way things work with JavaScript, where anything inside <script> tags is treated as JavaScript code. The main difference is that all your <script> tags are supposed to be placed within the page header, whereas PHP tags can occur anywhere within a page.

In the example, the PHP code contains a call to the echo function. This function prints out the value of whatever is passed to it. In this case, I passed the text "Hello world!" to the function, so that text is included in the page. The concept of functions should also be familiar to you from the lesson on JavaScript. Just like JavaScript, PHP lets you define your own functions or use functions built into the language. echo is a built-in function.

Statements in PHP, as in JavaScript, are terminated with a semicolon. (You can see the semicolon at the end of the statement in the example.) There's no reason why you can't include multiple statements within one PHP tag, like this:

<?php   echo "Hello ";   echo "world!"; ?>

PHP also provides a shortcut if all you want to do is print the value of something to a page. Rather than using the full PHP tag, you can use the expression tag, which just echoes a value to the page. Rather than using this:

<?php echo "Hello world!"; ?>

You can use this:

<?= "Hello world!" ?>

Replacing php with = enables you to leave out the call to the echo function and the semicolon. This style of tag is referred to as a short tag.

Sams Teach Yourself Web Publishing with HTML and CSS in One Hour a Day
Sams Teach Yourself Web Publishing with HTML and CSS in One Hour a Day (5th Edition)
ISBN: 0672328860
EAN: 2147483647
Year: 2007
Pages: 305

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: