PHP is a server-side technology that can use both client and server information to generate dynamic content in the form of XHTML pages. Much like ASP, PHP is interpreted by the server engine to determine the output. Unlike ASP, however, PHP is its own language scripting language and does not use VBScript or JavaScript as it's core. Additionally, PHP is an open source technology that is widely supported and used throughout the WWW. PHP is free to use and is supported by a vast, up-to-date and detailed online reference at www.php.net.


PHP syntax is very similar to that of the C programming language. Both are object-oriented languages and follow a lot of the same rules. Facebook.com is a great example of the power of PHP and how similar it is to C. Facebook utilizes PHP's speed and flexability to produce an interactive and fun experience for the user. AJAX calls are used to manipulate the PHP pages to increase the interactivity. For the deep inner-workings of Facebook, the PHP code was converted to C. By converting the backend functions to C, the code does not need to be interpreted or compiled and can therefore run quicker.


PHP also contains a very healthy relationship with the most popular databases technologies including MySQL, MSSQL, Oracle, SQLite and PostgreSQL. Additionally, PHP is cross-platform compatable, meaning you can use it on a IIS, Apache or Mac server stack. Popular server stacks are LAMP, WAMP and MAMP in which PHP is generally considered the "P" (sometimes Perl or Python is used, and some stacks include two or all three of these languages). These features give the language even more flexability and make it one of the most popular web technologies.

15.1 PHP Syntax

PHP is a loosely-typed programming language, like JavaScript, VBScript and ASP. This means you do not have to declare variable types, though it is very good practice to do so (especially for security reasons). PHP is generally places inside an XHTML document within <?php…?> tags. A shorthand version, <?…?> is allowed by some servers, but for cross-compatability, it is recommended that you use the full <?php…?> tags. Server configuration also allows the <%…%> tags to be used for PHP scripts, but this is off by default and is rarely used because of ASP confusion. Variables in PHP are preceeded with the $ symbol. Variable names must start with either an underscore or an upper or lowercase letter. Variable names cannot begin with a number. "$_3" is acceptable, "$3" is not. Just like JavaScript, the end of the statement character is the ;. Forgetting to include the ; can cause your page to not work correctly. Unlike JavaScript, however, PHP ignores white space and allows strings to use multiple lines without a connection character. Single or double quotes can be used to define a string. The echo language construct is commonly used to output the contents of a string or variable.

<?php
echo 'Hello World!';
?>

If you wanted to output the contents of a variable with a string, you would generally concatinate the variable wherever you wanted it. The . operator is placed in between the string and the variable. PHP also offers an alternative if you use double quotes instead of single quotes to enclose the string.

<?php
$insert = 'What a lovely day';
echo 'Hello World!' . $insert . ' it is!';

echo "Hello World! $insert it is!";
?>

Both of the echo statements above produce the exact same output.


Introduction to Web Design by Cynthia J. Martincic :: Credits