Please read the post Introduction to PHP, How to Insert Dynamic Content on WebPages, if you haven’t already.
Below is the code snippet from the previous post.
<html>
<body>
Date: <?php echo date("H:i A, jS F Y"); ?>
</body>
</html>
I’d like to state the following regarding the above code:
-
PHP code starts with a <?php and ends with a ?> tag. Though PHP supports some other tags (depending on the configuration) which signify the same but these are the most widely used and are guaranteed to work on most servers.
-
You can have any number of <?php …?> blocks in a file.
-
In PHP too, like C++ comments are written using // (single line) and /*…*/ (multi-line) symbols. Besides these one more symbol i.e. ‘#’ symbol can also be used for single line comments.
-
[Update: Added this point] Each statement of PHP ends with a semicolon ";" just like in C++. [/Update]
-
No need to bother about the arguments passed to the date() function now, it is just a format string which tells the date function which format to return the date in. eg. “H:i A, jS F Y” tells the date function to return date and time as “06:00 AM, 18th April 2008”. Echo command just echoes (prints) it to the screen.
-
Files containing PHP codes must have a “.php” extension (default) even if it contains HTML too.
-
Any file having an extension “.php” is parsed by PHP. As someone requests a PHP page, the server hands it over to PHP parser. PHP parses the file returning HTML codes as it is, but when it finds a <?php…?> block, it interprets the code between the tags and returns the result back. So we get something like this upon parsing the PHP code at the top of this post:
<html> <body> Date: 06:00 AM, 18th April 2008 </body> </html>
In this case, line 1 and 2 were returned as is without any interpretation whereas the PHP code between <?php…?> were interpreted and results were returned. Again line 4 and 5 were returned the same.
Related Articles: