Objective: Learn the foundational syntax and data handling structures of PHP — including variables,
data types, constants, and debugging functions.
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 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.
Variables in PHP store data. They always start with a $ sign and are case-sensitive.
<?php
$name = "Yogi";
$age = 30;
echo $name; // Outputs: Yogi
?>$$name ≠ $Name)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 |
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";Join strings using a dot (.):
$name = "Yogi";
echo "Hello " . $name . "!";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!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);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!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.
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.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!
| # | 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 |
Objective: Create a PHP page that introduces yourself using variables, constants, and HTML.
bio.php inside your php-basics folder.ACADEMY = "StreetGeek Academy".<!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.
variables.php → shows personalized messageconstants.php → uses both define() and constdebug.php → demonstrates strict comparisonbio.php → personal info using variables + constants
🏁 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.