How to Convert Boolean to String

How to Convert Boolean to String

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

Stefan Bogdanescu

Founder & Senior Architect

Why (string) Doesn't Give You "true" or "false" in PHP

When working with boolean values in PHP, the default casting behavior often surprises developers expecting a specific string representation. If you have a variable like $res = true and attempt to convert it using the cast operator (string)$res, you will receive "1". Similarly, casting false results in an empty string "". This is because PHP treats booleans as integers internally during type conversion: true equals 1 and false equals 0.

This behavior stems from the underlying C language heritage of PHP, where boolean types are often represented as integers. While this makes sense for arithmetic operations or conditional logic, it fails when your application requires a human-readable output like "true" or "false". Attempting to use non-existent functions like string($res) is a common misconception; PHP does not provide a built-in function named string() for type conversion.

The Ternary Operator Pattern

To achieve the desired format of "true" or "false", you need an explicit conditional check rather than a direct cast. The most straightforward approach is using a ternary operator. This pattern explicitly checks the truthiness of the variable and returns the corresponding string literal.

$res = true;
$converted_res = $res ? 'true' : 'false';

If $res is true, $converted_res becomes 'true'. If it is false, it becomes 'false'. This method is concise, readable, and leverages PHP's built-in logic without requiring external libraries. It works regardless of whether the boolean is set via direct assignment or derived from a conditional expression.

Using Conditional Expressions for Clarity

While the ternary operator is efficient, some teams prefer the expanded if statement structure for complex scenarios where additional logging or side effects might be needed. The logic remains identical: evaluate the boolean state and assign the appropriate string constant.

$res = false;

if ($res) {
    $converted_res = 'true';
} else {
    $converted_res = 'false';
}

This approach is more verbose but can improve readability in larger codebases where the ternary operator might obscure the flow of logic. It also makes it easier to insert debugging statements if you need to trace why a specific boolean value resulted in a particular string output.

String Comparison vs. Boolean Casting

A critical distinction exists between casting a boolean to a string and comparing a string to a boolean. If you have a user input string like "yes" or "no", you might want to normalize it into a boolean first before converting back to a human-readable format. However, if your goal is strictly to represent the internal state of a boolean variable as text for display purposes, direct casting is insufficient.

Attempting to rely on var_export() or print_r() will yield outputs like bool(true) which includes type hints and might not match your strict "true" requirement. These functions are designed for debugging output, not for generating clean data payloads. If you need to send this data to a frontend application or an API, you must ensure the format matches the expected JSON string representation, which aligns with the ternary approach described above.

Laravel Context

If you are working within the Laravel ecosystem, this conversion is frequently needed when building APIs that return JSON responses. Laravel's response helpers do not automatically convert booleans to "true"/"false" strings in JSON payloads; they stick to standard JSON boolean values (true/false). However, if your specific business logic dictates a string representation for legacy frontend compatibility or specific UI frameworks, you can apply the ternary pattern directly within your controller methods.

For example, when rendering a Blade view or constructing a JSON response manually:

public function show() {
    $isAvailable = true;
    
    // Convert boolean to custom string format for display
    $displayStatus = $isAvailable ? 'true' : 'false';
    
    return view('dashboard', compact('displayStatus'));
}

This ensures that the variable passed to the view contains exactly what is needed, avoiding any ambiguity in the template logic. Always verify that your frontend JavaScript receives the expected data type to prevent runtime errors during string concatenation or DOM manipulation.

Our network