需要帮助转换布尔值

this is actually WordPress but I think it is more of PHP question.

I am using SMOF framework (it is for theme option setting) and flexslider. TO combine bothe of them, in one of flexslider option, I have to use whether "true" or "false". The thing is, SMOF returns "0" and "1" instead of "true" or "false" - the word. So I need to convert "0" to "false" and "1" to "true"

I have this code:

function isBoolean($slider_loop) {
    $slider_loop = $smof_data['slider_loop'];
   if ($slider_loop === "true") {
      return true;
   } else {
      return false;
   }
   return $slider_loop;
}

..., and then in flexslider's "side":

animationLoop: "<?php echo isBoolean($slider_loop); ?>"

but it is not working.

Ideally, it should be something like this:

animationLoop: "<?php echo $smof_data['slider_loop'] 
// this return "0" and "1", while it should be "true" or "false" for flexslider to work ?>"

So, how should I do it?

Thanks in advance.

How about you take advantage of dynamic typing and type casting ? It's as simple as:

return (bool)$smof_data['slider_loop'];

..

replace

animationLoop: "<?php echo isBoolean($slider_loop); ?>"

with

animationLoop: "<?php echo ($slider_loop == 1) ? 'true' : 'false'; ?>"

If you echo true instead of "true", it will outputs 1 and nothing incase of false.

I'm not sure I understand the question completely. You say SMOF returns "0" and "1" but you make a check like this:

if($slider_loop === "true")

well, first "true" is not the same as true. One is string, the other boolean.

I think I would do it like this:

   $slider_loop = (int)$smof_data['slider_loop'];
   if ($slider_loop) {
      return true;
   } else {
      return false;
   }

1 and '1' are already truthy, and 0 and '0' are already falsy, so you can use them directly in your if statements. Read more about PHP Booleans.

If you need to echo out a string based on the bool, i.e., 'true' or 'false', you can simplify with the ternary operator:

echo $value ? 'true' : 'false';

In your case:

echo $smof_data['slider_loop'] ? 'true' : 'false';