Support »

Editing PHP code

There are times when you need to edit PHP code or add PHP code to an HTML page. The points below will help you do so without getting errors. This is not meant to be the last word on the subject but will keep you right in most instances. There are some cases when using FormToEmail that you might need to add PHP code to a page, such as when redirecting to a "thank you" page that shows the submitted form values, or displaying errors on the form page.

Not all of the items below are directly relevant to FormToEmail, but are provided for reference.

Basic requirements

First, for the PHP code to work, you need to have PHP installed on your server. The page/file you are editing, needs to have a .php extension, like "contact.php" or "index.php" for example. If your page is a straight HTML page and you want to put PHP code in it, then just rename it with a .php extension, so "contact_us.html" becomes "contact_us.php".

A PHP page/file can consist entirely of PHP code, or it can be a mix of code, like for example, HTML and PHP code on the same page.

PHP opening and closing tags

Any time PHP code is used in a file, it must be contained within opening and closing tags. These are the tags:

<?php ?>

<?php is the opening tag. ?> is the closing tag.

The tags can be on the same line as the code, they can be at either end of a very long file, they can be at either end of a block of code. So long as the PHP code is preceded by an opening tag and ended with a closing tag, it doesn't matter where the tags are. These two examples, both do the same thing:

<?php $my_email = "[email protected]"; ?>

$my_email = "[email protected]";

If you need to add PHP code to a page, you should be mindful of the opening and closing tags. Say for example, your page is a straight HTML page with some form code on it. Because it doesn't have any PHP code in it, you will need to enclose your PHP code in opening and closing tags, like this:

some PHP code

some HTML code

In the above example, some PHP code is followed by some HTML code. That is fairly common. You will also get cases where you need to do some PHP code, then some HTML code, then some more PHP code. In which case, you would need to "open and close" PHP, like so:

some PHP code

some HTML code

some more PHP code

some more HTML code

If you are adding new lines of PHP code to an existing PHP page (or block of PHP code in an HTML page), you will already have the opening and closing tags there, so you would add your new lines between the existing tags.

If you put tags within tags, you will get an error. The following code will give you an error:


<?php $my_email = "[email protected]"; ?>


Starting a session

Some PHP routines require a session to be used. This is the code for starting a session in PHP:

<?php session_start(); ?>

You MUST start the session BEFORE any output from the page. Otherwise the code will fail. This usually means putting the session_start() code at the top of the file. Suppose you are using session_start() on a form page. The following code will give you an error:

the HTML form code

<?php session_start(); ?>

This is correct:

<?php session_start(); ?>

the HTML form code

Typically, an error will contain this message: "Cannot send session cookie - headers already sent" and/or this: "Cannot send session cache limiter - headers already sent".

You don't need to add session_start(); to your code if it is already there at the top of the file. This means if you are pasting in some code that contains session_start();, then you don't need the session_start(); part if your code already has it at the top of the file.

Assigning values to variables

Here's an example of assigning a value to a variable:

$subject = "Comments from contact form";

In the above example, $subject is the variable and "Comments from contact form" is the value assigned to the variable. The value in this case is text, so it is enclosed in quotes. A variable can have a numeric value, like this:

$auto_redirect = 0;

Note that a numeric value does not have quotes. You can also have an empty value, like this:

$redirect_url = "";

In such a case, the variable is set but it does not have a value.

Semi-colon at end of statements

Any code statement like a function call or assigning a value to a variable, must end with a semi-colon, like so:

$message = "Hello World!";

print $message;

There is an exception to this where the statement is the last line, or only line of code. Using the semi-colon in all instances does no harm, and most PHP programmers do so.

Single quotes or double quotes

Text can be quoted using single quotes or double quotes. Technically PHP handles these differently but in most instances, single or double quotes can be used. In many cases it's a question of personal style/preference, so this:

$banned_ip_message = "Your IP address is banned.  The form was not sent."; the same as this:

$banned_ip_message = 'Your IP address is banned.  The form was not sent.';

Closing brackets and quotes

PHP code commonly uses brackets ( ), square brackets [ ] and curly brackets { }. These are always used in pairs. For every opening bracket, you should have a closing bracket, like this example:

if(!$set){$errors[] = "You cannot send a blank form";}

Same too for quotes, single or double, if you have opening quotes, you should have corresponding closing quotes. Like this:

$headers = "From: {$from_name} <{$_REQUEST['email']}>";

Escaping quotes

If you need to print quote marks (or assign them to a variable) with PHP, you must escape them with a backslash, like so:

print "The word \"banana\" is in quotes";

Joining variable values and text

There are times when using PHP code that you need to join variable values with text values. This is known as concatenation. A period is used to separate the variable from the text. Each segment of text is in quotes. Here's an example:

$name = "Bob";

print "Today " . $name . " will host the meeting.";

Note the use of spaces in the quoted text so that the name displays properly. Using variables within text can also be done by using curly brackets around the variable. Doing so for the above example means that you don't need to concatenate it with periods, thus:

$name = "Bob";

print "Today {$name} will host the meeting.";

Code comments

PHP code can include comments. Comments get ignored by PHP. There is no need to remove comments from a PHP file. The code will work EXACTLY the same way with or without the comments. A comment line is preceded by two forward slashes in the PHP code, like so:

// This is a comment.

The above comment applies to one line. If you want to make a comment of multiple lines, use this syntax (using forward slashes and asterisks):


Another comment on a new line.
Last line of comments.