Get random boolean true/false in PHP

Get random boolean true/false in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Get random boolean true/false in PHP

When you need a random boolean value in PHP, the most straightforward approach often involves casting an integer to a boolean. A common snippet you might encounter looks like this:

$value = (bool) rand(0, 1);

While this works, relying on rand() for cryptographic security is generally ill-advised. However, if your use case is purely non-cryptographic—such as simulating a coin flip in a game logic script or generating random test data—the casting method is perfectly valid and efficient. The core question here isn't whether casting introduces disadvantages, but rather understanding the underlying mechanism and ensuring you're using the right tool for the specific context of randomness required.

How Casting Works Under the Hood

In PHP, type juggling is automatic and predictable. When you cast an integer to a boolean, PHP follows strict rules defined in its language specification:

  • 0 becomes false.
  • Any non-zero integer (e.g., 1, 2, -5) becomes true.

Therefore, (bool) rand(0, 1) effectively returns false if the random number is 0 and true if it is 1. Since rand() has a roughly 50/50 chance of returning either value within that range, the resulting boolean distribution is statistically sound for general purposes. There are no hidden side effects or performance penalties associated with this casting operation in standard PHP versions; it is a fast, native operation handled at the engine level.

The Security Caveat: rand() vs. random_int()

The primary technical pitfall to avoid when generating random booleans isn't the casting itself, but the source of the randomness. If your application requires cryptographic security—say, for generating session tokens, password resets, or secure lottery draws—the built-in rand() function is insufficient. It is a legacy function that can be predicted if the initial seed is known and relies on system time, which isn't suitable for security-sensitive contexts.

For security-critical scenarios, you should switch to random_int(). This function uses the operating system's cryptographically secure pseudo-random number generator. The implementation looks like this:

// Secure random boolean
$secureValue = (bool) random_int(0, 1);

Here, we still cast the result to boolean, but now we are leveraging a secure entropy source. random_int() was introduced in PHP 7 and is available in modern versions up to the current stable releases. If you are running an older version of PHP (pre-7.0), you would need alternative methods like mcrypt_create_iv or external libraries, though updating your PHP version is usually the preferred solution.

Why Not Just Return the Integer?

You might wonder if there is a scenario where returning the integer directly makes sense. In strict type-checking scenarios using declare(strict_types=1);, returning an integer from a function expecting a boolean will trigger a TypeError. To ensure your code remains robust and compatible with stricter coding standards, explicit casting is not just a stylistic choice; it is a requirement for type safety.

Casting ensures that the variable's type hint matches its actual value. If you have a function defined as function flipCoin(): bool, passing an integer to it without casting will fail under strict typing rules. The cast (bool) acts as a bridge, guaranteeing the return type aligns with the method signature without altering the logical outcome of the random selection.

Conclusion

For non-security contexts, (bool) rand(0, 1) is a concise and effective pattern that leverages PHP's automatic type conversion without disadvantage. For security-sensitive applications, replace rand() with random_int() while maintaining the cast to satisfy strict typing requirements. The choice depends entirely on the entropy source you need rather than the casting mechanism itself.

Our network