Skip to content
PHP Logo

How can a string with 0 and 1 be transformed into a Boolean expression in PHP?

To transform a string with 0s and 1s into a Boolean expression in PHP, we can use the ternary operator to check if the string is '1' or '0' and convert it to true or false accordingly. We can then use this Boolean value in our code for further processing.

php
$string = '1'; // example string with 1
$boolean = ($string === '1') ? true : false; // transform string into boolean expression

var_dump($boolean); // output: bool(true)