AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Communication Technology

PHP basics: echo, variables and constants

Week 10 · Unit 3 Creating Site Pages (3), Topic 1 (continued): PHP code in a page, echo and print, comments, variables, quotes and types, constants · 3 lessons
1

PHP tags, echo and comments

ICT Secondary 2 Term 1, Unit 3, Topic 1: pp. 78–82
GoalWrite a PHP block, print text with echo and add comments.
New words
PHP tag · the pair <?php and ?> that marks where PHP code starts and endsecho · the PHP command that prints text, which goes into the pagesemicolon · the sign ; that ends every PHP statementcomment · a note for people that PHP skips: // for one line, /* … */ for several lines
Explanation

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.

Php
1<?php
2echo "Welcome to the dictionary";
3?>
Output
Welcome to the dictionary
Worked examples
Php
1<?php
2// one-line comment: this line is skipped
3echo "Hana";
4/* a comment
5   over several lines */
6echo " and ";
7echo "Omar";
8?>
Output
Hana and Omar
Two echo statements and two kinds of comment. PHP skips the comments. The statements run in order, one after the other, and print their text next to each other. Read it, predict the output, and then check.
Line as typedProblemFixed
<? phpa space after the question mark<?php
echo "Hi"no semicolonecho "Hi";
/* note / *the comment is closed wrongly/* note */
echo 'Welcome";the quotation marks do not matchecho "Welcome";
Omar checks four lines against the rules. Each wrong line has a one-sign fix.
Class activity

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.

Practice
1
What is printed by: echo "A"; echo "B";
2
What does PHP do with a comment?
3
How do you start and end a block of PHP code?
4
What ends every PHP statement, and what happens if you forget it?