π§© 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.