# Lecture 1: PHP Backend Development - Environment Setup & PHP Basics **Duration:** 3 Hours **Trainer:** Fadi Ramzi **Course:** PHP Backend Development using Laravel (Upcoming)\ **Target Audience:** Beginners and Fresh Graduates --- ## Objectives By the end of this session, trainees will be able to: - Set up a modern PHP development environment on Windows. - Use Visual Studio Code (VS Code) effectively for PHP development. - Understand PHP fundamentals with real-world examples. --- ## Section 1: Development Environment Setup (60 mins) ### 1.1 Recommended Environment: XAMPP (Modern Beginner-Friendly Choice) > In 2025, XAMPP remains a fast, practical tool to get beginners started quickly with PHP + MySQL + Apache in one package. **Steps:** 1. Download XAMPP from [https://www.apachefriends.org/index.html](https://www.apachefriends.org/index.html) 2. Install it to the default path (e.g., `C:\xampp`) 3. Open the XAMPP Control Panel: - Start Apache - Start MySQL 4. Test PHP installation: - Go to `C:\xampp\htdocs` - Create a file `test.php` with: ```php <?php phpinfo(); ?> ``` - Open your browser: [http://localhost/test.php](http://localhost/test.php) > ✅ You now have a working local web server + PHP runtime ready to execute scripts. ### 1.2 Visual Studio Code Setup - Download: [https://code.visualstudio.com/](https://code.visualstudio.com/) - Recommended Extensions: - **PHP Intelephense** — Smart PHP intellisense - **PHP Debug (Later)** — Debug with breakpoints - **Prettier** — Optional, for formatting --- ## Section 2: PHP Basics & Fundamentals (1 hour 30 mins) > Each concept below includes a real-world use case and a breakdown to make it beginner-friendly. ### 2.1 Introduction to PHP - PHP stands for **Hypertext Preprocessor** - Server-side scripting language - Powers major platforms (WordPress, Laravel, Magento) - Executes on the server and sends HTML back to the browser ### 2.2 PHP Syntax & Tags **Example:** ```php <?php echo "Welcome to our website!"; ?> ``` **Explanation:** - `<?php ... ?>` tells the server to execute the code inside as PHP. - `echo` outputs the message. - Variables prefixed with `$` inside strings will be parsed (interpolated). ### 2.3 Variables and Data Types **Example:** ```php <?php $product = "Laptop"; // String $price = 999.99; // Float $quantity = 2; // Integer $inStock = true; // Boolean echo "$product costs \$$price."; ?> ``` **Explanation:** - `$` is used to declare all variables. - PHP supports dynamic types: string, int, float, bool, null. - Use `\$` to escape the dollar sign inside double-quoted strings. ### 2.4 Strings & String Functions **Example:** ```php <?php $name = "John Doe"; echo strlen($name); // Length echo strtoupper($name); // Uppercase echo strtolower($name); // Lowercase echo str_replace("Doe", "Smith", $name); // Replace echo substr($name, 0, 4); // Partial ?> ``` **Explanation:** - Strings are commonly used for usernames, messages, file paths. - Use built-in string functions to manipulate content safely and efficiently. ### 2.5 Constants (define vs const) **Example 1 - Using **``**:** ```php <?php define("SITE_NAME", "MyShop"); echo SITE_NAME; ?> ``` **Example 2 - Using **``**:** ```php <?php const CURRENCY = "USD"; echo CURRENCY; ?> ``` **Explanation:** - `define()` is old-style, allows defining constants in global scope only. - `const` is modern and can be used inside classes too. Preferred in modern PHP. ### 2.6 Arrays (Basics & Functions) **Indexed Array:** ```php <?php $fruits = ["Apple", "Banana", "Mango"]; echo $fruits[1]; // Banana ?> ``` **Associative Array:** ```php <?php $user = ["name" => "Ali", "role" => "admin"]; echo $user["role"]; ?> ``` **Array Functions:** ```php <?php $numbers = [10, 20, 30]; echo count($numbers); // 3 echo array_sum($numbers); // 60 var_dump(in_array(20, $numbers)); // true ?> ``` **Explanation:** - Arrays hold groups of values (products, users, etc.). - Useful for grouping, looping, and managing related data. ### 2.7 Control Structures (if, else, elseif) **Example:** ```php <?php $stock = 0; if ($stock > 0) { echo "In stock"; } else { echo "Out of stock"; } ?> ``` **Explanation:** - Executes code conditionally based on values. - Common in product availability, user permissions, logic branching. ### 2.8 Loops (foreach, for) with Arrays **Example:** ```php <?php $cart = ["Laptop", "Mouse", "Keyboard"]; foreach ($cart as $item) { echo "Item: $item<br>"; } ?> ``` **Explanation:** - Loops let us repeat actions over lists. - Often used for displaying arrays of data like product listings. ### 2.9 Functions (Detailed) **Example:** ```php <?php function greetCustomer($name) { return "Hello, $name!"; } echo greetCustomer("Ali"); ?> ``` **Explanation:** - `function` defines reusable code. - Functions may take parameters (inputs) and return values (outputs). - Use meaningful names, camelCase style (e.g., `calculateTotal`, `getUserInfo`). --- ## Homework / Practice ## ✅ Environment Setup Task (Must Complete Before Lecture 2) ### Task 1: Local Environment Validation 1. **Download & Install the following:** - [XAMPP](https://www.apachefriends.org/index.html) - [Visual Studio Code](https://code.visualstudio.com/) 2. **Start Apache** using the **XAMPP Control Panel**. 3. **Create a file** inside: ``` C:\xampp\htdocs\hello.php ``` 4. **Paste this PHP code in `hello.php`:** ```php ```php <?php echo "Hello from PHP!"; ?> ``` 5. Open your browser and visit: ``` http://localhost/hello.php ``` 6. Expected Output: ``` Hello from PHP! ``` ## 🧠 Hands-on Practice Task (Covers All PHP Basics) ### Task 2: Build a Simple Product Display Page Create a file named `product.php` and write code to demonstrate all PHP basics you’ve learned: --- ### 🔹 1. Define Variables - Define the following variables: - `$productName` - `$price` - `$quantity` - `$category` - `$isAvailable` (boolean) --- ### 🔹 2. String Manipulation Use at least **three string functions** from this list: - `strtoupper` - `strtolower` - `strlen` - `str_replace` - `substr` Apply them to `$productName` or `$category`. --- ### 🔹 3. Array Handling - Create an **indexed array** of three product names - Use array functions: - `count()` - `in_array()` --- ### 🔹 4. Control Logic - Write an `if...else` block to: - Check if the product is available - Show: `"In Stock"` or `"Out of Stock"` - Use a `foreach` loop to display the list of products in your array --- ### 🔹 5. Functions Create a function named `calculateTotal($price, $qty)` that: - Multiplies the price by the quantity - Returns the result Call this function and print the result using `echo`. --- --- ## Next Lecture (Lecture 2 Preview) - PHP Object-Oriented Programming - Classes, Objects, Inheritance - Introduction to Laravel Framework (installation and structure) --- ## Useful Resources - PHP Manual: [https://www.php.net/manual/en/](https://www.php.net/manual/en/) - W3Schools PHP: [https://www.w3schools.com/php/](https://www.w3schools.com/php/) - Laracasts (Free Laravel Tutorials): [https://laracasts.com](https://laracasts.com) --- Prepared by: Fadi Ramzi Date: 25-06-2025