CHAPTER 04
Beginner
PHP Syntax and Backend Basics
Updated: May 14, 2026
20 min read
# CHAPTER 4
PHP Syntax and Backend Basics
1. Introduction
Now that your local server is running, it's time to learn the grammar of the PHP language. Like all programming languages, PHP uses variables to store data, conditional statements to make decisions, and loops to automate repetitive tasks. In this chapter, we will cover the foundational syntax of PHP, providing the building blocks for complex backend logic.2. Learning Objectives
By the end of this chapter, you will be able to:-
Write basic PHP syntax and use the
echostatement.
- Declare variables and understand basic data types.
-
Write conditional logic using
if/elsestatements.
-
Use
forandwhileloops to process repetitive tasks.
3. Beginner-Friendly Explanation
Think of programming syntax like the rules of grammar in English. In English, a sentence must end with a period. In PHP, an instruction must end with a semicolon (;).
Variables are like labeled storage boxes. You create a box labeled $age, put the number 25 inside it, and whenever you need that number, you just ask for the $age box.
Conditional statements are like a bouncer at a club: *"If you are older than 18, go in. Else, go home."*
4. Basic Syntax and Echo
Every PHP script begins with<?php.
The echo command is used to output text (or HTML) to the browser.
php
5. Variables and Data Types
In PHP, all variables start with a dollar sign ($). Unlike some languages, PHP is "loosely typed," meaning you don't have to declare if a variable is a number or text; PHP figures it out.
php
6. Conditional Logic (If / Else)
Backend logic often involves making decisions based on data.
php
7. Loops (Automating Tasks)
Loops are used to execute the same block of code a specified number of times. This is incredibly useful for generating HTML lists or tables from database records.
php
8. Backend Workflow Example: A Simple Paywall
Let's combine variables and conditions to simulate a backend paywall logic.
php
9. Best Practices
-
Naming Conventions: Use clear, descriptive variable names. Use camelCase (
$userAge) or snake_case ($user_age), but pick one and stay consistent throughout your entire application.
10. Common Mistakes
-
The Missing Semicolon: The #1 error beginners face is a completely broken page resulting from a "Syntax Error" or "Parse Error." 99% of the time, it is because you forgot to put a semicolon
;at the end of the previous line of code.
11. Exercises
-
1.
Write a PHP script with a variable
$temperature. Write anif/else if/elseblock that prints "It's freezing!" if below 32, "It's nice!" if between 32 and 80, and "It's hot!" if above 80.
12. Coding Challenges
-
Challenge: Write a
forloop that prints the multiplication table of 7 (e.g., "7 x 1 = 7", "7 x 2 = 14", all the way up to 10).
13. MCQs with Answers
Q1. In PHP, which symbol is used to start a variable name? A) @ B) # C) $ D) % *Answer: C*
Question 2
What is the correct way to concatenate (join) a string and a variable in PHP?
14. Interview Questions
-
Q: Explain the difference between
==and===in PHP conditional statements.
-
Q: Describe a scenario in a web application where a
whileloop would be more appropriate than aforloop.
15. FAQs
Q: Can I use single quotes instead of double quotes for strings? A: Yes, but there is a difference. Double quotes parse variables inside them (echo "Hello $name";), whereas single quotes print exactly what is typed (echo 'Hello $name'; will literally print the text $name to the screen).