PHP Syntax

Dec 03, 2008 Author: City Hall

Saving a PHP file

A PHP file is basically a text file containing all the required PHP code to run a specific program. PHP code can be written with the use of a simple text editor, like Windows Notepad, or the more useful Crimson Editor. All you need to do is type in the required PHP code and save the file with the extension .php

You can create a PHP file yourself by creating a blank text file and simply renaming it with the extension .php. You need absolutely nothing else for this to work as a valid PHP file. Although, as I’m sure you’ll realise, a blank file won’t do anything much of use! First of all, we need to enter some valid PHP code into this file

(Important: Don’t use a program such as Microsoft Word, Frontpage or other more complex word processors to create your PHP file. Although the page might look free of formatting when you save the file, there will be other hidden formatting elements within the file itself that will prevent any PHP code from running.)

This is an example of some PHP code

//this code is saved in a file named e.g. myphp.php and then
//saved in e.g. c:\local\mywebsite\myphp.php or
$x = 1;
$y = "b";
echo "Hello world";

This code is the exact same code that will work as a PHP file on a server, and we’ll take a look shortly at how you can create your very own simple PHP file to run as a basic web-page sample. First though, we need to know where to locate our PHP files.


The location of the PHP file on disk

An important thing for us at this point is to know where to save this .php file for it to be recognised as a valid web-page file. Any PHP documents must be placed within the document root of a website server. In simple terms, this means that on your computer, or on the remote computer that you’ve got set up as a website host, are a number of folders and directories. One of these directories will be set up as the document root, and is usually named c:\local if you’re using your own Windows machine to run the code, or something like /htdocs or /html_root if you’re running on a remote server.

The document root is the base directory from which all the website data is served from. Any sub-directories within this main root directory will show up as sub-folders when you type in the URL

e.g. if you have a folder “mysite” within the c:\local\ folder, you’ll be able to access the URL http://localhost/mysite to see the contents of this folder. Any PHP files within this folder can be accessed by adding the PHP filename to the end. e.g. http://localhost/mysite/myphpfile.php

Similarly with a remote host, a folder like htdocs/mysite will be accessible from and any relevant PHP files in the mysite folder will be at and so on

Continue to the next part of this Tutorial

tags: php syntax

views 4735
  1. Add New Comment