
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to convert string to boolean in PHP?
To convert string to boolean in PHP, the code is as follows. Use the filter_val() method−
Example
<?php echo "Displaying Sunday as first day of a week...
"; $res = date('l - d/m/Y', strtotime("sunday 0 week")); echo "First day (next week) = ", $res."
"; var_dump(filter_var($res, FILTER_VALIDATE_BOOLEAN)); ?>
Output
This will produce the following output−
Displaying Sunday as first day of a week... First day (next week) = Sunday - 15/12/2019 bool(false)
Example
Let us now see another example −
<?php var_dump(filter_var('0', FILTER_VALIDATE_BOOLEAN)); var_dump(filter_var('1', FILTER_VALIDATE_BOOLEAN)); var_dump(filter_var('-0', FILTER_VALIDATE_BOOLEAN)); var_dump(filter_var('', FILTER_VALIDATE_BOOLEAN)); ?>
Output
This will produce the following output−
bool(false) bool(true) bool(false) bool(false)
- Related Articles
- How to convert Boolean to String in JavaScript?
- How to convert String to Boolean in JavaScript?
- JavaScript Convert a string to boolean
- Java Program to convert String to Boolean
- Java Program to convert Boolean to String
- Golang Program to convert Boolean to String
- Haskell Program to Convert Boolean to String
- How to convert a boolean value to string value in JavaScript?
- How can I convert a string to boolean in JavaScript?
- Convert Java String Object to Boolean Object
- How to convert array to string PHP?
- Swift program to convert boolean variable into string
- C++ Program to convert Boolean Variables into String
- Golang Program to convert boolean variables into string
- Haskell Program to convert boolean variables into string

Advertisements