How to teach php programming to a beginner

Someone says they want to learn PHP, and the instinct is to open with a diagram of how a web server works. Resist it. The first session should end with a page they wrote themselves, can refresh in a browser, and can change without asking permission. Everything else — syntax rules, arrays, databases — attaches to that page later.

Get one page running before explaining anything

Install PHP locally, or use the built-in server from the project folder with a single command in the terminal. Then create a file, type a line that prints a greeting, and open it in the browser. That first refresh, where text they typed appears on a page they own, does more for motivation than any introduction to the language.

Explain only what is needed to keep the page working: files end in the PHP extension, PHP code sits between opening and closing tags, and the server is what turns that file into HTML. Three facts, one working page, done.

Introduce variables through a form

A form is the shortest path from PHP as a printing machine to PHP as something that responds. Build a page with one text field and a submit button, then read the submitted value and print it back inside a sentence. Now variables, strings, and request data all appear at once, attached to something visible.

Have them change the greeting three times — a different word, a different punctuation mark, a second field for a surname. Each change is small enough to type by hand and immediate enough to see. Show htmlspecialchars at this point, not later. Escaping output is easier to accept as part of the pattern on day one than as a correction on day nine.

Let them break it on purpose

Delete a semicolon. Misspell a variable name. Remove a closing brace. Each time, refresh the page, read the error message out loud, and fix it together. The lesson is that PHP tells you where it gave up, and that the line number in the message is a starting point rather than an accusation.

  • Turn on error reporting while working locally, so warnings are visible instead of silent.
  • Read the first line of the error, not the whole wall of text.
  • Look at the line above the reported line too — the mistake is often there.
  • Keep the previous working version in a comment or a copy before experimenting.

Doing this deliberately removes the fear that stops people from touching their own code.

Assign homework that fits in twenty minutes

Long exercises get abandoned, and abandoned homework teaches the wrong lesson. Give three small tasks that all modify the same page: change the greeting based on the time of day, add a checkbox that changes the message, print the submitted name in capitals. Each one needs a single new idea and a couple of lines.

Then stop the lesson while the page still works. A beginner who leaves with a running page and three small ideas for it will come back next week. One who leaves with a notebook full of theory and a broken file will not.

Teach the next session from their questions, not from a syllabus. What they tried to change on their own is the most accurate guide to what to explain next.