🧩 Module 2: PHP Syntax and Basic Concepts
Objective: Learn the foundational syntax and data handling structures of PHP — including variables,
data types, constants, and debugging functions.
🔹 1. PHP Tags and Comments
Every PHP file starts and ends with PHP tags:
<?php
// Your code goes here
?>
These tags tell the server where PHP code begins and ends. Anything outside them is treated as plain HTML.
💬 Comments
Comments let you explain your code and are ignored by PHP when running.
<?php
// Single-line comment
# Also a single-line comment
/*
Multi-line
comment
*/
?>
🧠 Best practice: Use comments to describe what your code does — especially when defining functions or logic.
🔹 2. Variables
Variables in PHP store data. They always start with a $ sign and are case-sensitive.
<?php
$name = "Yogi";
$age = 30;
echo $name; // Outputs: Yogi
?>
🧩 Naming Rules:
- Must start with
$ - Must begin with a letter or underscore
- Cannot contain spaces or start with a number
- Are case-sensitive (
$name≠$Name)
🔹 3. Data Types
PHP automatically determines a variable’s type based on its value. The main data types are:
| Type | Example | Description |
|---|---|---|
| String | "Hello, StreetGeek" |
Text data |
| Integer | 25 |
Whole number |
| Float / Double | 3.14 |
Decimal number |
| Boolean | true or false |
Logical value |
| Array | [1, 2, 3] or array("red", "blue") |
Multiple values |
| NULL | NULL |
Empty or undefined variable |
| Object | new ClassName() |
Instance of a class |
🔹 4. Constants
Constants are like variables — but their values cannot change once defined.
<?php
define("SITE_NAME", "StreetGeek Academy");
echo SITE_NAME;
?>
You can also use:
const VERSION = "1.0";
🔹 5. String Concatenation & Interpolation
🧩 Concatenation
Join strings using a dot (.):
$name = "Yogi";
echo "Hello " . $name . "!";
🧩 Interpolation
If you use double quotes, variables are automatically inserted into strings:
$name = "Yogi";
echo "Hello $name!";
Single quotes (' ') do not parse variables:
echo 'Hello $name!'; // Outputs literally: Hello $name!
🔹 6. Debugging with var_dump()
Debugging means understanding what data your variables actually hold.
$city = "Richmond";
var_dump($city);
Output example:
string(8) "Richmond"
You can also inspect multiple values:
$name = "Yogi";
$age = 30;
$is_student = true;
var_dump($name, $age, $is_student);
🧩 Hands-On Practice
Exercise 1: Variables and Strings
Create a new file named variables.php in your php-basics folder and add:
<?php
$name = "Yogi";
$language = "PHP";
echo "Hello $name, welcome to $language programming!";
?>
✅ Output should be:
Hello Yogi, welcome to PHP programming!
Exercise 2: Data Type Practice
Add this code below your first example:
<?php
$name = "StreetGeek Academy";
$students = 100;
$rating = 4.9;
$is_open = true;
$launch_year = NULL;
var_dump($name, $students, $rating, $is_open, $launch_year);
?>
✅ Check the output — PHP tells you the data type and value of each variable.
Exercise 3: Constants and Concatenation
Create a file named constants.php:
<?php
define("ACADEMY", "StreetGeek Academy");
const FOUNDED = 2025;
echo "Welcome to " . ACADEMY . "!";
echo "<br>";
echo "Established in " . FOUNDED . ".";
?>
✅ Output:
Welcome to StreetGeek Academy!
Established in 2025.
Exercise 4: Debug Challenge
Create a script debug.php with these lines:
<?php
$students = "50";
$enrolled = 50;
if ($students === $enrolled) {
echo "They are equal.";
} else {
echo "They are not equal.";
}
?>
✅ What happens?
=== checks for both value and type, so "50" (string) ≠ 50 (integer).
🧠 Takeaway: Always be aware of your variable types!
📝 Module 2 Quiz
| # | Question | Options | Correct |
|---|---|---|---|
| 1 | What symbol must all variables begin with? | a) # · b) $ · c) % | b |
| 2 | Which function displays data type and value? | a) print() · b) var_dump() · c) inspect() | b |
| 3 | How do you define a constant? | a) set(“PI”, 3.14) · b) const PI = 3.14 · c) define(“PI”, 3.14) | c |
| 4 | What is the result of 'Hello $name' if $name = "Yogi"? |
a) Hello Yogi · b) Hello $name · c) Error | b |
| 5 | What is the difference between == and ===? |
a) None · b) == compares values, === compares values and types | b |
💪 Challenge Task – Personal Bio Page
Objective: Create a PHP page that introduces yourself using variables, constants, and HTML.
- Create a new file
bio.phpinside yourphp-basicsfolder. - Add variables for your:
- Name
- Age
- Favorite language
- Hobby
- Create a constant called
ACADEMY = "StreetGeek Academy". - Output a small HTML profile page like this:
<!DOCTYPE html>
<html>
<head><title>About Me</title></head>
<body>
<h1>Welcome to <?php echo ACADEMY; ?></h1>
<p>My name is <?php echo $name; ?>.</p>
<p>I’m <?php echo $age; ?> years old and I love coding in <?php echo $language; ?>.</p>
<p>My favorite hobby is <?php echo $hobby; ?>.</p>
</body>
</html>
✅ Bonus: Use var_dump() to check the data types of your variables at the end of the page.
🧾 Submission Checklist
- ✅
variables.php→ shows personalized message - ✅
constants.php→ uses bothdefine()andconst - ✅
debug.php→ demonstrates strict comparison - ✅
bio.php→ personal info using variables + constants - ✅ Quiz completed
🏁 Next Step: In Module 3, you’ll use what you’ve learned here to perform calculations and logic with PHP —
mastering operators and expressions to build functional scripts like a discount calculator.