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
107: Random Number Functions: random_int, mt_rand
Imagine you're organizing a giveaway for your community. For a low-stakes prize, like a free sticker, you might just put a bunch of names in a hat and shake it up. It's fast, it's "random enough," and if someone somehow predicted which name you'd pull, it's not the end of the world. But if you're generating a password reset token or a high-stakes lottery winner, you can't just "shake a hat." You need a process that is mathematically impossible to predict, even for someone who knows exactly how your system works.
In PHP, we map these two scenarios to two different functions: mt_rand() is our "shaking the hat" method, and random_int() is our "cryptographically secure" method.
The Fast and Loose Approach with mt_rand
When you're building something like a game where a monster has a 20% chance to drop a rare item, you don't need military-grade security. You just need something fast that feels random to the player. That's where mt_rand() comes in. It uses the Mersenne Twister algorithm, which is significantly faster and "more random" than the ancient rand() function you might see in some legacy tutorials (seriously, just ignore rand() entirely).
// Simulating a critical hit in a game
$chance = mt_rand(1, 100);
if ($chance <= 10) {
echo "Critical hit! Massive damage!";
} else {
echo "A regular hit.";
}
The only catch? mt_rand() is pseudo-random. If someone knows the "seed" (the starting point) and the algorithm, they can technically predict every single number that will come next. For a game, that's a minor bug; for a security feature, that's a catastrophe.
When Security is Non-Negotiable with random_int
If you're dealing with anything involving money, passwords, or user identity, you must use random_int(). Unlike its cousin, this function generates cryptographically secure pseudo-random integers. It gathers "entropy" from your operating system—basically, it looks at unpredictable system noise—to ensure the number it picks is truly unpredictable.
I always tell my junior devs: if the number you're generating is going to be sent to a user as a secret, use random_int(). No exceptions.
// Generating a 6-digit verification code for an email sign-up
try {
$verificationCode = random_int(100000, 999999);
echo "Your secure code is: " . $verificationCode;
} catch (\Exception $e) {
// In very rare cases, the system might not have enough entropy
echo "Could not generate a secure number: " . $e->getMessage();
}
Choosing Your Tool
To keep it simple, I use a quick mental checklist when deciding which one to reach for:
- Is this for a visual effect, a game mechanic, or a non-sensitive shuffle? →
mt_rand(). - Is this for a token, a password, a session ID, or any security-related value? →
random_int().
One last tip: random_int() is slightly slower than mt_rand() because it has to do more work to be secure. In 99% of your web apps, you won't even notice the difference, but it's good to know why it exists.
📋 Practical Task
Build a Secure One-Time Password (OTP) Generator
Your task is to create a script that simulates a secure account verification system. You need to build a "Verification Service" that does the following:
- Generates a cryptographically secure 6-digit integer (between 100,000 and 999,999).
- Stores that number in a variable.
- Prints a message to the screen that looks like:
"Security Alert: Your one-time verification code is: [number]. This code expires in 10 minutes." - Wraps the generation logic in a try-catch block to handle any potential
Exceptionthatrandom_int()might throw if the system lacks sufficient entropy.
There are no comments for now.