How to convert string to boolean in PHP?
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:
- var: It is the required field. It denotes the variable to filter.
- filterName: It is used to specify the ID or name of the filter to use. The default filter is FILTER_DEFAULT. It is optional field.
- options: It is used to specify one or more flags/options to use. Check each filter for possible options and flags. It is also optional field.
Return Value: Returns the filtered data on success, or False on failure.
Program:
<?php // PHP program to illustrate the conversion // of String to Boolean value // The below statement returns the boolean value true var_dump(filter_var('true', FILTER_VALIDATE_BOOLEAN)); var_dump(filter_var('1', FILTER_VALIDATE_BOOLEAN)); var_dump(filter_var('on', FILTER_VALIDATE_BOOLEAN)); var_dump(filter_var('yes', FILTER_VALIDATE_BOOLEAN)); // The below statement returns the boolean value false var_dump(filter_var('false', FILTER_VALIDATE_BOOLEAN)); var_dump(filter_var('0', FILTER_VALIDATE_BOOLEAN)); var_dump(filter_var('off', FILTER_VALIDATE_BOOLEAN)); var_dump(filter_var('no', FILTER_VALIDATE_BOOLEAN)); var_dump(filter_var('', FILTER_VALIDATE_BOOLEAN)); ?> |
bool(true) bool(true) bool(true) bool(true) bool(false) bool(false) bool(false) bool(false) bool(false)
Recommended Posts:
- JavaScript | Convert a string to boolean
- Convert boolean result into number/integer in JavaScript
- How to convert a string into number in PHP?
- How to convert an object to string using JavaScript?
- Convert string into date using JavaScript
- How to convert string into float in JavaScript?
- Convert a string to an integer in JavaScript
- Convert a String to a List of Characters in Java
- How to convert string to camel case in JavaScript ?
- Convert a String to Character array in Java
- Convert string to title case in JavaScript
- Convert List of Characters to String in Java
- How to convert a currency string to a double value with jQuery or Javascript?
- JavaScript | Convert bytes to human-readable string
- How to convert seconds to time string format hh:mm:ss using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



