将php函数返回值转换为true / false

时间:2022-12-04 19:55:59

I first referred to this this, but it did not solve my problem.

我首先提到这个,但它没有解决我的问题。

I am having something like:

我有类似的东西:

  $message .=   "\n\tWhether $testOutputDir is a directory:" . (!!is_dir($testOutputDir))
                . "\n\tWhether $outputDir is a directory:" . is_dir($outputDir)
                . "\n\tWhether $outputDir is readabale:" . is_readable($outputDir)
                ....

I just want to print something as follows:

我只想打印如下内容:

       Whether /a is a directory: true
       Whether /b is a directory: true

But it prints as follows:

但它打印如下:

       Whether /a is a directory: 1
       Whether /b is a directory: 1

Any suggestions to solve this?

有什么建议可以解决吗?

EDIT:

I could check func == 1 ? TRUE : FALSE. But I am expecting a simple cast or similar.

我可以检查func == 1吗?真假。但我期待一个简单的演员或类似的。

3 个解决方案

#1


1  

In PHP, when a boolean value is converted to a string, you get '1' (for true) and '' (empty string, for false). If you want otherwise, you're going to have to explicitly convert the boolean value to a string.

在PHP中,当一个布尔值转换为一个字符串时,你得到'1'(对于true)和''(空字符串,对于false)。如果您不想要,则必须将布尔值显式转换为字符串。

There is no cast that will get you the result you want. One way to solve your problem would be to pass your value into this function:

没有任何演员可以获得你想要的结果。解决问题的一种方法是将您的值传递给此函数:

function stringForBool($bool) {
    return ($bool ? 'true' : 'false');
}

//use like:
echo stringForBool(isReadable($outputDir));

You could also inline this function directly in your code, rather than calling it, but if you're using it more than a few times, that would become awfully repetitive.

你也可以直接在你的代码中内联这个函数,而不是调用它,但如果你使用它多次,那就会变得非常重复。

Other answers suggest using json_encode(). While that certainly works (if you pass in a boolean value), you will not get the expected output if you pass in something that isn't exactly true or false. You can, of course, call json_encode((bool)$yourValue), that that will give you what you want, but (in my opinion) it's a little more magical and a little less explicit.

其他答案建议使用json_encode()。虽然这肯定有效(如果你传入一个布尔值),如果传入的东西不完全正确或错误,你将无法获得预期的输出。当然,你可以调用json_encode((bool)$ yourValue),这会给你你想要的东西,但(在我看来)它更神奇,更不明确。

#2


0  

You'd be stuck doing this:

你会被困在这样做:

is_dir($outputDir) ? 'true' : 'false'

Converting a bool to a string in PHP always converts it to "" or "1".

在PHP中将bool转换为字符串始终将其转换为“”或“1”。

Now, this is a bit of a hack, but you can actually just use json_encode():

现在,这有点像黑客,但你实际上只能使用json_encode():

php > var_dump(json_encode(true));
string(4) "true"

#3


0  

This may work,

这可能有用,

echo json_encode(true);  // string "true"

echo json_encode(false); // string "false"

So,

 $message .=   "\n\tWhether $testOutputDir is a directory:" . json_encode(!!is_dir($testOutputDir))
                . "\n\tWhether $outputDir is a directory:" . json_encode(is_dir($outputDir))
                . "\n\tWhether $outputDir is readabale:" . json_encode(is_readable($outputDir))

#1


1  

In PHP, when a boolean value is converted to a string, you get '1' (for true) and '' (empty string, for false). If you want otherwise, you're going to have to explicitly convert the boolean value to a string.

在PHP中,当一个布尔值转换为一个字符串时,你得到'1'(对于true)和''(空字符串,对于false)。如果您不想要,则必须将布尔值显式转换为字符串。

There is no cast that will get you the result you want. One way to solve your problem would be to pass your value into this function:

没有任何演员可以获得你想要的结果。解决问题的一种方法是将您的值传递给此函数:

function stringForBool($bool) {
    return ($bool ? 'true' : 'false');
}

//use like:
echo stringForBool(isReadable($outputDir));

You could also inline this function directly in your code, rather than calling it, but if you're using it more than a few times, that would become awfully repetitive.

你也可以直接在你的代码中内联这个函数,而不是调用它,但如果你使用它多次,那就会变得非常重复。

Other answers suggest using json_encode(). While that certainly works (if you pass in a boolean value), you will not get the expected output if you pass in something that isn't exactly true or false. You can, of course, call json_encode((bool)$yourValue), that that will give you what you want, but (in my opinion) it's a little more magical and a little less explicit.

其他答案建议使用json_encode()。虽然这肯定有效(如果你传入一个布尔值),如果传入的东西不完全正确或错误,你将无法获得预期的输出。当然,你可以调用json_encode((bool)$ yourValue),这会给你你想要的东西,但(在我看来)它更神奇,更不明确。

#2


0  

You'd be stuck doing this:

你会被困在这样做:

is_dir($outputDir) ? 'true' : 'false'

Converting a bool to a string in PHP always converts it to "" or "1".

在PHP中将bool转换为字符串始终将其转换为“”或“1”。

Now, this is a bit of a hack, but you can actually just use json_encode():

现在,这有点像黑客,但你实际上只能使用json_encode():

php > var_dump(json_encode(true));
string(4) "true"

#3


0  

This may work,

这可能有用,

echo json_encode(true);  // string "true"

echo json_encode(false); // string "false"

So,

 $message .=   "\n\tWhether $testOutputDir is a directory:" . json_encode(!!is_dir($testOutputDir))
                . "\n\tWhether $outputDir is a directory:" . json_encode(is_dir($outputDir))
                . "\n\tWhether $outputDir is readabale:" . json_encode(is_readable($outputDir))