Senin, 18 Juli 2011

PHP Tutorial Part 7

Introduction

In the past 6 parts of this tutorial I have shown you the basics of writing PHP. In this final part I will show you a few small things which don't really warrant a section of their own.

Comments

As with any programming language, it is quite important to comment in your script. If you are working on a 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 it. 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.

In PHP there are two ways 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:

/* 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, all of these do the same thing and you can use any or all of them in a script. There is no reason to even use the same type all through a script. The only problem you may find is that, as I 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 me 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 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 always remember to close IF statements and loops, though, as it is very easy to forget.

One Line Prints

Being able to place HTML code into your PHP 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

This tutorial has given you some of the basics of PHP and should allow you to do most things you will want to. 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 with a database. As this is one of the major reasons that people use PHP and because there are many options
I will put this in a separate PHP/MySQL tutorial.



source "

Free Template Blogger collection template Hot Deals BERITA_wongANteng SEO theproperty-developer

1 komentar:

Evee Faranisa mengatakan...

wow this theme a very amazing success for your site, thanks a
obat keloid bismilah http://acemaxs31.com/obat-keloid/

Poskan Komentar

"my facebook"