Setting up PHP

PHP is a free programming language
Most web hosts offer PHP (and MySQL) support, so you can start using PHP immediately.

However, some servers do not support PHP and so some additional steps must be taken.

  1. Make sure a web server is installed (Learn how to install one here)
  2. Install PHP (Go to PHP.net for installation instructions)
  3. Install a database (For example: MySQL)
After completion of these steps PHP can be used.
Save as .php files into your web directory.
The server will parse them automatically.

How to create a PHP file

To open a file in PHP, use the fopen() function.
To use this function, 2 pieces of information are necessary.
  1. Give the name of the file that needs to be opened
  2. State what the file is going to do once opened (what mode it should be opened into)

File Name

3 pieces of code can name the file.
  1. < ?php This is the opening php tag.
  2. $file=fopen("hello.txt","w"); The file thats going to be opened is called hello.txt. This code opens the file, and writes the letter w (the mode).
  3. ?> This is the closing php tag.

File Purpose or File Mode

The mode dictates what can be done to the file after it is opened. These are the types of modes a file can be opened in.
  • r : read only (at the beginning of the file)
  • r+ : read and write (at the beginning of the file)
  • w : write only (opens and clears the file , or creates a new file if it doesn't exist)
  • w+ : read and write (opens and clears the file , or creates a new file if it doesn't exist)
  • a : append (opens and writes to the end of the file, or creates a new file if it doesn't exist)
  • a+ : read and append (writes to the end of the file to save the content)
  • x : write only (creates a new file, or sends an error back if the file already exists)
  • x+ : read and write (creates a new file, or sends an error back if the file already exists)

Write html using php

Combining html and php can help create a more complex website.
As stated above, < ?php and ?> are the opening and closing tags for PHP.
A page with php and html is an html page with portions of php.
Here is an example.:
opening html tag
opening and closing header tags in html
opening body tag
Hello, today is < ?php echo date('l, F jS, Y'); ?>
closing body tag
closing html tag