Chapter 12
Introducing Ajax and PHP
IN THIS CHAPTER
- Mixing PHP with HTML
- Echoing text to the browser
- Working with variables
- Working with arrays
- Using if statements
- Using switch statements
- Using for loops
- Using while loops
- Using foreach loops
Ajax developers work on the server as well as in the browser, of course, and the most common server-side language used with Ajax these days is PHP. Accordingly, this chapter and Chapter 13 give you an introduction to using PHP with Ajax.
Getting Started with PHP
You can embed your PHP scripts inside HTML pages if you enclose the scripts in special markup. In code, you should enclose your PHP scripts (which are stored in files with the extension .php) inside the markup <?php and ?> like this:
<?php . . . PHP goes here . . . ?>
However, in practice, you can use <? and ?>, and the server will still understand:
<? . . . PHP goes here . . . ?>
A PHP-enabled server will execute the PHP code inside the <?...?> markup. Here's an example that runs the built-in PHP function phpinfo, phpinfo.php, which creates an HTML table that tells you about your PHP installation.
Note in particular how the HTML and PHP are interspersed in this example, phpinfo.php. Also note that as in JavaScript, you end each PHP statement with a semicolon (;).
<html> <head> <title> A first PHP example </title> </head> <body> <h1> A first PHP example </h1> <? phpinfo(); ...
Get Ajax Bible now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.