O'Reilly logo

Ajax Bible by Steven Holzner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

images 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(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required