Skip to main content
PHP Backend Development Tutorial
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 echo statement.
  • Declare variables and understand basic data types.
  • Write conditional logic using if/else statements.
  • Use for and while loops 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
1234567891011
<?php
// This is a single-line comment. The server ignores this.

/* 
This is a 
multi-line comment.
*/

echo "<h1>Welcome to my App</h1>";
echo "This is standard text.";
?>

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
123456789
<?php
$name = "John Doe";      // String (Text)
$age = 30;               // Integer (Whole number)
$price = 19.99;          // Float (Decimal)
$is_admin = true;        // Boolean (True/False)

// Concatenation: Joining text and variables using a dot (.)
echo "My name is " . $name . " and I am " . $age . " years old.";
?>

6. Conditional Logic (If / Else)

Backend logic often involves making decisions based on data.
php
12345678910111213141516
<?php
$user_role = "admin";
$account_balance = 50;

// Basic If/Else
if ($user_role == "admin") {
    echo "Welcome to the Admin Dashboard.";
} else {
    echo "Welcome to the User Profile.";
}

// Complex Conditions (&& means AND, || means OR)
if ($account_balance < 100 && $user_role != "admin") {
    echo "Low balance warning.";
}
?>

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
12345678910111213141516
<?php
// A 'For' Loop: runs exactly 5 times
echo "<ul>";
for ($i = 1; $i <= 5; $i++) {
    echo "<li>Item Number: " . $i . "</li>";
}
echo "</ul>";

// A 'While' Loop: runs as long as the condition is true
$inventory = 3;
while ($inventory > 0) {
    echo "<p>Selling an item. Items left: " . $inventory . "</p>";
    $inventory--; // Subtract 1 from inventory
}
echo "<p>Sold out!</p>";
?>

8. Backend Workflow Example: A Simple Paywall

Let's combine variables and conditions to simulate a backend paywall logic.
php
1234567891011121314
<?php
$has_subscription = false;
$article_preview = "The stock market crashed today due to...";
$full_article = "The stock market crashed today due to rising interest rates and inflation.";

echo "<h2>Financial News Daily</h2>";

if ($has_subscription) {
    echo "<p>" . $full_article . "</p>";
} else {
    echo "<p>" . $article_preview . " <b>[Read More...]</b></p>";
    echo "<a href=&#039;subscribe.php'><button>Subscribe to Read Full Article</button></a>";
}
?>

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. 1. Write a PHP script with a variable $temperature. Write an if/else if/else block 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 for loop 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 while loop would be more appropriate than a for loop.

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).

16. Summary

In Chapter 4, we learned the foundational grammar of PHP. Variables act as data containers, conditional statements allow our applications to make intelligent decisions based on that data, and loops automate repetitive output. By mastering these basics, you can build simple logical workflows, such as checking user permissions or generating dynamic HTML lists.

17. Next Chapter Recommendation

Logic is great, but right now our code is isolated. We need users to interact with it. Proceed to Chapter 5: Handling Forms and User Input.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·