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. The points below are written with the FormToEmail form processor script in mind, whereby you might need to edit PHP code, or add PHP code to an HTML form page to use some of the additional features of the script.

Basic requirementsFirst, 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 tagsAny 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 = "mail@example.com"; ?>

<?php

$my_email = "mail@example.com";

?>

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:

<?php

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:

<?php

some PHP code

?>

some HTML code

<?php

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. This will give an error:

<?php

<?php $my_email = "mail@example.com"; ?>

?>

Starting a sessionSome of the features in the script, and items in the support section, 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 script/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. This will give an error:

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.

Editing arraysArrays are often used in PHP code and are used a lot in FormToEmail. In the Pro version of the script, FormToEmail-Pro, some of the configurable options entail putting values into an array. This line of code from FormToEmail-Pro shows an array in use:

$required_fields = array('name','email','comments');

In the above code, 'name','email' and 'comments' are the array values. In some cases, you might want to remove values from the array, in other instances you might want to add values to the array. In either case, use the same format as above. Put the values in quotes and separate values with a comma. Here's an array with one value:

In the above examples, the way the array is written is purely down to style. The code is exactly the same and does the same thing in both cases.

Assigning values to variablesConfiguring an option in the script is usually done by assigning a value to a variable, like this:

$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 statementsAny 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 quotesText can be quoted using single quotes or double quotes. Technically PHP handles these differently but in all instances of quoted text in FormToEmail and FormToEmail-Pro, 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.";

...is the same as this:

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

Closing brackets and quotesThe PHP code in the script 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} ";

Escaping quotesIf 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 textThere 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 commentsPHP code can include comments. The instructions in the script are 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):