PHP lesson 5: Joining strings: . and quotes
US version. US grades and US spelling. · UK version of this lesson
PHP joins text with a DOT: "Hi " . $name. The + sign always means ADD, so "5" + "3" is 8 but "5" . "3" is "53". This is a trap for anyone who knows JavaScript or Python. A complete 40-minute PHP lesson (Grades 9–12), from Unit 2: Numbers and strings. It runs right here in your browser – nothing to install.
- Time
- 40 minutes + 20 min homework
- Grade
- Grades 9–12 (ages 14–18)
- You need
- A computer or tablet, paper and a pencil
- Graded
- Instantly, with explanations
For parents: how to use this lesson
You don't need to know PHP yourself. The lesson explains each step on screen, the way Mr Babbage, our Head of Computing, likes it: a worked example first, then a go for your child. Your child predicts what code prints, traces it and puts it in order – graded straight away – and then writes and fixes real PHP in the practice test, where it runs in the browser.
- Read what your child will learn together.
- Press Start the lesson below. Sit with your child for the teaching and the worked example (about 16 minutes): ask them to say what each line does.
- Let them do the practice on their own, then the write-it and fix-it questions in the practice test. When code goes wrong, PHP's message is a clue – let them read it out and try again before you help.
- Finish with the talk question: When would you choose single quotes on purpose?
Watch out for
+joins strings. In PHP+adds; joining is..- Single and double quotes are the same. Only double quotes put the variable's value in.
Miss Lovelace's tip for home: ask your child to explain one line of their program to you in their own words. If they can explain it, they understand it – and every message from PHP is a clue, not a failure.
What your child will learn
Learning goal: join strings with the dot . and .=.
Success looks like
- I can join strings with the dot
.and.=. - I can put variables inside double-quoted strings.
- I can explain the difference between single and double quotes.
Key words
concatenation · dot operator · interpolation · single quotes · double quotes · escape sequence
What to have ready
- A device with a web browser (PHP runs in the browser – nothing to install)
- Paper for tracing
- A keyboard helps for typing code (a tablet or phone works too)
The big ideas (for the adult)
PHP joins text with a DOT: "Hi " . $name. The + sign always means ADD, so "5" + "3" is 8 but "5" . "3" is "53". This is a trap for anyone who knows JavaScript or Python.
Inside DOUBLE quotes, PHP puts a variable's value in: "Hi $name" prints Hi Sam. Use {$name} when letters follow straight after: "{$item}s". Inside SINGLE quotes, nothing is replaced: 'Hi $name' prints Hi $name.
$s .= "!"; adds text to the end of $s. The special codes \n (new line) and \t (tab) work only in double quotes.
Worked example – see it
<?php echo "7" . "6";
It prints:
76
The lesson, step by step
| Part | Time | How it is checked |
|---|---|---|
| Starter | 5 min | Graded by Classfern |
| Teaching input | 8 min | Read, watch and talk |
| See it, then predict | 8 min | Graded by Classfern |
| Put it in order, trace it, check it | 14 min | Graded by Classfern |
| Closure and exit ticket | 5 min | Graded by Classfern + self-check |
The lesson
It runs here on the page. No sign-up and no names – progress is kept only in this browser. Each visit picks a new mix of questions.
Homework
20 minutes: 7 questions – predict what code prints, trace it or put it in order, and check the key ideas – then a short reflection. New questions every time, all graded instantly.
Write it and fix it: practice tests
The practice tests run real PHP in your browser: write and fix short programs, graded against hidden tests.
Curriculum
CSTA K–12 Computer Science Standards (2017): 2-AP-11 – Create clearly named variables of different data types and operate on them; 3B-AP-24 – Compare programming languages and their suitability for different problems.
The standard codes are CSTA's; the descriptions are our own words.
Our lessons and questions are our own. They are not written or endorsed by any government department, testing agency or exam board. Classfern is not affiliated with or endorsed by The PHP Group. PHP runs here as php-wasm, under its open-source licences.