|
PHP notes:
As you have seen, using forms with PHP script can be very effective. In the next part we will show you some of the final things you should know about PHP language.
Introduction
In the past given parts of this PHP tutorial we have seen the basics of writing PHP script. In this final part we will show you a few small things which don't really warrant a section of their own.
Comments
As with any of the programming languages we use, it is quite important to comment these in your script. If you are working on a PHP script with someone else you must let them know what you code does and if you are distributing your script you will need to show people how to edit this. Even if you are the only one who will use your script it is useful to comment so that you can edit it at a later date for updation.
In PHP script there are two ways through which you can comment. One way is used for single line comments and the other is used mainly for comments that go over one line. A single line comment is written as follows:
// Your comment can go in here
Everything after the // will be ingnored when the script is executed. You can even place these on the end of another line e.g.
print "Hello $name"; // Welcome to the user
Another way of commenting is by using multi-line comments process:
/* The following piece of code will take the input
the user gave and will check that it is valid before
adding it to the database */
Anything between the /* and the */ will be ignored. It is important that you always close this type of comment as not doing so could make your script not work.
Print, Echo and HTML
As you may have noticed during this tutorial I have actually used 4 different ways of outputting information to the browser:
echo("Text here");
echo "Text here";
print("Text here";
print "Text here";
To clarify that, all of these do the same thing and you can use any or all of them in a PHP scripting language. There is no reason to even use the same type all through a script. The only problem you may find is that, as we explained in part 2, all the " in the HTML code must be replaced with \" which, if you have a lot of code, could take a very long time. This brings us to a very useful part of PHP. If, for example, you created the header of a page dynamically in PHP, then had the static page and finally a dynamic footer you can do the following:
<?
Top PHP code in here
?>
HTML Code
<?
Bottom PHP code in here
?>
This gets even better as the PHP script code will just continue from where it was left off so you could do the following:
<?
IF Statement {
?>
HTML For IF Being Correct
<?
} else {
?>
HTML For IF Being Wrong
<?
}
?>
You must have to remember to close IF statements and loops, though, as it is very easy to forget.
One Line Prints
This being able to place HTML code into your PHP language is very useful, but what happens if you want to put the value of a variable into the code. Unlike when using an echo or print statement, you can't just put in the variable name as this section is not actually part of the PHP code. Instead you must just put in a little PHP.
For example if you wanted to print someone's name from a script with HTML formatting you would do the following:
<font face="Arial" size="7" color="red"><b><? echo($variablename); ?></b></font>
In the above code you have just added in the following PHP:
<? echo($variablename); ?>
Which is exactly the same as the following PHP code:
<?
echo($variablename);
?>
But all put onto one line.
Conclusion The result is, this tutorial has given you some of the basics of PHP script and should allow you to do most things you will want to do on php. For a much more in depth look you should visit PHP.net, the official homepage of PHP. One major omission of this tutorial, you may have noticed, is using PHP script with a database. As this is one of the major reasons that people use PHP and because there are many options
We will put this in a separate PHP/MySQL tutorial. |