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
241: Interactive CLI Prompts in PHP
How do I actually capture a string from the terminal?
When you're moving from web requests to CLI scripts, the first thing you'll realize is that $_POST and $_GET are gone. To get a response from a user, you have two main options: fgets(STDIN)` and readline().
In my experience, readline() is the way to go for most interactive prompts. It's cleaner and handles the newline character for you, whereas fgets()` leaves that annoying \n at the end of the string, forcing you to call trim() every single time. Here is how a basic prompt looks:
<?php
$env = readline("Which environment are you deploying to? (staging/production): ");
echo "Preparing deployment for $env...\n";
?>
Just a heads-up: readline() requires the readline extension to be enabled in your php.ini. If you're on a locked-down server where you can't change the config, fall back to trim(fgets(STDIN))`βit does the exact same thing, just with a bit more typing.
How do I keep asking until the user gives me a valid answer?
Users are unpredictable. They'll hit Enter by accident or type "maybe" when you specifically asked for "yes" or "no". I never trust the first response. The best pattern here is a while(true) loop that only breaks once the input meets your criteria.
Let's say we're building a script to configure a database port. We can't just take any string; it has to be a number. I usually handle it like this:
<?php
while (true) {
$port = readline("Enter the database port [default 3306]: ");
// Handle the default value if they just hit enter
if (empty($port)) {
$port = 3306;
break;
}
if (is_numeric($port) && $port > 0 && $port < 65536) {
break; // Input is valid, exit the loop
}
echo "Invalid port. Please enter a number between 1 and 65535.\n";
}
echo "Port set to: $port\n";
?>
Can I hide the input for sensitive data like API keys?
This is where PHP gets a little gritty. PHP doesn't have a built-in readPassword() function. If you use readline() for an API key, the key will stay visible on the screen in plain text, which is a security nightmare if someone is looking over the user's shoulder.
To get around this on Unix-based systems (Linux/macOS), you have to talk to the terminal directly to turn off "echoing." I use shell_exec to toggle the stty setting. It's a bit of a hack, but it's the industry standard for simple PHP CLI tools.
<?php
echo "Enter your Secret API Key: ";
// Turn off echoing of characters
shell_exec('stty -echo');
$apiKey = trim(fgets(STDIN));
// Turn echoing back on immediately!
shell_exec('stty echo');
echo "\nKey captured successfully.\n";
// Now you can use $apiKey safely
?>
Be very careful here: if your script crashes between stty -echo and stty echo, the user's terminal will remain "silent," and they'll think their keyboard is broken. Always make sure your echoing is restored.
π Practical Task
Build a CLI Project Initialization Tool
Create a PHP script that acts as a "Project Starter." The script must interactively collect the following information from the user to create a simulated config file:
- Project Name: Must not be empty. If the user hits enter, prompt them again until a name is provided.
- Project Type: Must be either 'web' or 'api'. If they enter anything else, tell them the options and ask again.
- Database Password: Must be captured using the
stty -echomethod so the password doesn't appear on the screen.
Once all three are collected, the script should print a summary of the "Config File" it created (e.g., Project: MyCoolApp | Type: api | Password: [HIDDEN]).
There are no comments for now.