Syntax error How to convert string to boolean in PHP?

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

 Live Demo

<?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 −

 Live Demo

<?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)
Updated on: 2019-12-24T12:22:19+05:30

763 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements