PHP tags, echo and comments
Remember Week 2: the server runs PHP and sends the browser only what the code prints. PHP code sits in a block that starts with <?php, written with no space, and ends with ?>. Everything outside the block is plain HTML and is sent as it is. Inside the block, echo prints text. Every statement ends with a semicolon; forgetting it is the most common mistake, and PHP stops with an error message. Text is written between quotation marks. Comments are notes for people, and PHP skips them: // starts a comment that lasts to the end of the line, and /* … */ makes a comment over several lines, and it must be closed with */. Good comments say why you did something. Remember also that PHP words such as echo are not the result: the visitor sees only the text that was printed.
1<?php 2echo "Welcome to the dictionary"; 3?>
Welcome to the dictionary
1<?php 2// one-line comment: this line is skipped 3echo "Hana"; 4/* a comment 5 over several lines */ 6echo " and "; 7echo "Omar"; 8?>
Hana and Omar
| Line as typed | Problem | Fixed |
|---|---|---|
| <? php | a space after the question mark | <?php |
| echo "Hi" | no semicolon | echo "Hi"; |
| /* note / * | the comment is closed wrongly | /* note */ |
| echo 'Welcome"; | the quotation marks do not match | echo "Welcome"; |
On paper write a PHP block that prints three lines of text with three echo statements and has one comment of each kind. Then underline the semicolons, circle the PHP tags, and write what the visitor will see.