Wednesday, February 4, 2009

structure of php

print "Hello Web!";
?>

In this simple script you can see some of the most used components of a PHP script. First off, PHP tags are used to separate the actual PHP content from the rest of the file. You can inform the interpreter that you want it to execute your commands by adding a pair of these: standard tags ""; short tags ""; ASP tags "<% %>"; script tags "". The standard and the script tags are guaranteed to work under any configuration, the other two need to be enabled in your "php.ini"

Now that you know how to define a block of PHP code, take a closer look at the code above. The "print" function is used to output data, so anything output by "print()" ends up in the HTML file. Therefore, you can say that a function is a command that performs an action. Usually, you send some data to the function, and the function uses that data to come up with a result. There are a lot functions in PHP, and almost each one performs a different action. Data sent to a function is almost always placed in parentheses after the function name; there are some exceptions where parentheses are optional, and the "print()" function is one of them.

After that first line of code, you can see a semicolon. This semicolon informs the interpreter that you have completed a statement - a statement is to PHP what a sentence is to the English language. It represents an instruction to the interpreter, and some additional data. If PHP doesn't find a semicolon at the end of your statement, then it will continue parsing the file until it finds one, ignoring any white-spaces or empty lines. So, your one statement doesn't necessarily have to use only one line of code. There can be two or more statements on a single line, but, on the other hand, a statement could use two or more lines. PHP also ignores white spaces, so you can have as many blanks as you want between the statements, and between the statements' parameters. You should know that you don't have to use a semicolon with the last statement in your script (just before the closing tag). So the following scripts are equivalent:

print "This is a test" ;
?>



Commenting you PHP code can be very helpful. If some code seems to be very clear at the time of writing, the same code can look like a black hole a few weeks later, when you want to modify it. So adding comments to your code can save you time later on, and make it easier for other people to work with your code. But, wait, what is a comment? A comment is a text in a script that is ignored by the interpreter. So you can write anything you want in it, from copyright notices to detailed information about your code. PHP recognizes a comment by checking out if it contains two forward slashes ("//") or a single hash sign ("#"). The text beginning from either of these marks until the end of the line is ignored. You can also use multi-line comments. They begin with a forward slash followed by an asterisk ("/*") and end with an asterisk followed by a forward slash ("*/"):

/*
This is a demo script.
All it does is output Hello Web! in your browser.
*/

print "Hello Web!"; //outputs a message

//copyright (C) SoftwareProjects.org 2004
?>

No comments:

Post a Comment