build-website-header
spacer-image
 

Starting PHP- Part 2

Continued from part1

In this section we are going to look at the basic building blocks of PHP.

File Extensions

PHP files are usually given a .php file extension. The file extension tells the web server to pass the file to the php interpreter rather than just displaying it.

However it is possible to configure the server to treat any file extension as a php file and pass it to the php interpreter. This is useful when moving from a static site to a dynamic site or/and if you want the visitors to see .htm file extensions as that is what many are used it.

This is accomplished on apache servers by editing the .htaccess file. The exact method will vary according to your implementation. Here is the code for you need to add to the .htaccess files for 1and 1.

#parsing PHP in html files
#
AddType x-mapp-php4 .html .htm

#parsing .php as .php4 files
 

PHP Statements

PHP Statements end with a semi colon (;) eg

<?php
echo "hello this is php code interpreted by the php interpreter";
?>

Also just as for html extra white space characters are ignored so you can use newlines, tab and spaces to make your code more readable.

PHP Variables

A variable contains data which can be changed.

  • PHP variables are case sensitive
  • PHP variables must start with a letter or underscore "_".
  • PHP variables consist of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
  • Variables are designated by a dollar sign as the first character.
  • There is no need to define the variable type it is done automatically on assignment.
  • PHP variables are normally available from with in function only, but can be made available within a script (global) or across all scripts (super global).

PHP Constants

Constants contain data that cannot be changed. They are created using the built-in define function.

e.g

define("MAXIMUM", 20);

sets MAXIMUM equal to 20. By convention constants are in capitals.

Putting it together

The code

<?php
define("MAXIMUM", 20);
$count=30;
echo "variable is =". $count ."<
br>";
echo "constant is=". MAXIMUM;
?>

Produces

variable is =30
constant is=20

 

as output

Google
Web www.build-your-website.co.uk