How to convert string to boolean in PHP? (original) (raw)

Last Updated : 11 Jul, 2025

Given a string and the task is to convert given string to its boolean. Use filter_var() function to convert string to boolean value.Examples:

Input : $boolStrVar1 = filter_var('true', FILTER_VALIDATE_BOOLEAN); Output : true

Input : $boolStrVar5 = filter_var('false', FILTER_VALIDATE_BOOLEAN); Output : false

Approach using PHP filter_var() Function: The filter_var() function is used to filter a variable with specified filter. This function is used to both validate and sanitize the data.Syntax:

filter_var( var, filterName, options )

Parameters: This function accepts three parameters as mentioned above and described below:

Return Value: Returns the filtered data on success, or False on failure.Program:

php `

`

Output:

bool(true) bool(true) bool(true) bool(true) bool(false) bool(false) bool(false) bool(false) bool(false)