HTML and CSS Reference
In-Depth Information
How PHP Works
PHP enables programmers to include PHP code in their HTML documents, which is
processed on the server before the HTML is sent to the browser. 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 supports PHP,
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:
<!DOCTYPE html>
<head><title> A PHP Page </title</head>
<?php echo “Hello world!”; ?>
If this page is requested from a web server that supports PHP, the HTML sent to the
browser will look like this:
<!DOCTYPE html>
<head><title> A PHP Page </title</head>
Hello world!
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. PHP code is distinguished 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.
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 in to the language. echo is a built-in function.
Search WWH ::

Custom Search