PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
123: Compact and Extract Functions
Why would I use compact() instead of just building an array manually?
I've seen a lot of developers write out associative arrays like this: $data = ['name' => $name, 'email' => $email, 'role' => $role];. It's explicit, sure, but it's tedious and repetitive. If you have five or six variables, you're just typing the same names twice.
compact() is your shortcut. It looks for variables in the current symbol table that match the strings you pass into it and builds the array for you. Here is how I'd handle a user profile setup:
$userName = 'jdoe_dev';
$userEmail = 'jane@example.com';
$userStatus = 'Active';
// Instead of the long way, just do this:
$userProfile = compact('userName', 'userEmail', 'userStatus');
print_r($userProfile);
/*
Output:
Array (
[userName] => jdoe_dev
[userEmail] => jane@example.com
[userStatus] => Active
)
*/
It's a huge time-saver, especially when you're passing a bunch of local variables into a template rendering function or a view helper.
How does extract() work and when is it actually useful?
If compact() is the "packer," extract() is the "unpacker." It takes an associative array and imports the keys as variable names. It's essentially the inverse of the first function we talked about.
You'll see this most often in custom template engines or configuration loaders. Imagine you've fetched a row from a database as an array, but you want to use those values as standalone variables in your HTML template to keep the code clean.
$productData = [
'title' => 'Mechanical Keyboard',
'price' => 120.00,
'stock' => 15
];
extract($productData);
// Now I can use $title, $price, and $stock directly
echo "The {$title} costs ${$price}. We have {$stock} left.";
I'll be honest: it feels like magic, and in software, "magic" can sometimes be a red flag. But when used for a clean separation between your logic and your presentation layer, it's very effective.
Is there a catch to using extract()?
Yes, a big one. You have to be extremely careful about where the array comes from. If you extract($_POST) or extract($_GET), you are letting a user create variables inside your script. If you already had a variable called $isAdmin set to false, and a user sends ?isAdmin=1 in the URL, extract() will overwrite your security check.
To prevent this, PHP provides flags. I always recommend using EXTR_SKIP if you're not 100% sure about the source of the data. This tells PHP: "If the variable already exists, don't touch it."
$isAdmin = false;
$userData = ['isAdmin' => true, 'nickname' => 'Hacker123'];
// Use EXTR_SKIP to protect existing variables
extract($userData, EXTR_SKIP);
echo $isAdmin ? 'Admin access' : 'Standard user';
// Output: Standard user (the array didn't overwrite our boolean)
Basically, use compact() as much as you like, but treat extract() with a bit of healthy suspicion.
📋 Practical Task
Exercise: Building a Product Detail Page Data Bridge
You are building a simple product display system. You have a set of individual variables representing a product, but your renderProductPage() function requires a single array as an argument. Once inside that function, you want to unpack that array back into variables for easier printing.
Your Task:
- Create three variables:
$productName,$productPrice, and$productSku. - Use
compact()to wrap these three variables into a single array called$productBundle. - Define a function
renderProductPage($data). Inside this function:- Use
extract()on the$dataarray. - Print a string using the extracted variables (e.g., "Product: [name], SKU: [sku], Price: [price]").
- Use
- Call the function and pass in your
$productBundle.
There are no comments for now.