哪一个是将True值设置为函数参数的正确和更好的方法

时间:2022-11-19 11:42:30

Following are two line of code, please observe them.

以下是两行代码,请注意它们。

$client->setWatermarkInBackground('True');
$client->setWatermarkInBackground(True);

From the code lines you might have an idea that I want to send a value True as an argument. But I'm not getting the difference between above two statements. Can anyone explain me the difference between two statements and which one is correct or wrong? If both the ways are correct then which statement is better to use? etc. Please explain me. Thanks in advance.

从代码行中你可能有一个想法,我想发送一个值True作为参数。但我没有区分上述两个陈述。任何人都可以解释我两个陈述之间的区别,哪一个是正确的还是错误的?如果两种方法都正确,那么哪种语句更好用?等我请解释一下。提前致谢。

5 个解决方案

#1


0  

$client->setWatermarkInBackground('True');

You're setting the value true as a string and not the correct Boolean.

您将值设置为字符串而不是正确的布尔值。

You should set it like this (2nd method) as to set boolean ( 0/1 ).

你应该像这样(第二种方法)设置它来设置布尔值(0/1)。

$client->setWatermarkInBackground(True);

Allowing you to do something like:

允许你做类似的事情:

if($client->setWatermarkInBackground()) {
    // is true do whatever you need?
}

#2


0  

There is no difference, since PHP promotes the undefined name True to the string "True". You should instead be using the lowercase literal true

没有区别,因为PHP将未定义的名称True提升为字符串“True”。您应该使用小写字面值true

#3


0  

'True' is the string True, in english.
True is the boolean value that means true for the computer.

'True'是字符串True,用英语表示。 True是布尔值,对计算机来说意味着true。

Although PHP is very good and understands both as the computer value, there are some differences:

虽然PHP非常好并且将它们理解为计算机价值,但是存在一些差异:

  • When using the test if ($client->getWatermarkInBackground() === true), the first line will return false, because the triple = means "Same value and same type", which is not the case, a string doesn't equal a boolean.
  • 当使用测试if($ client-> getWatermarkInBackground()=== true)时,第一行将返回false,因为triple =表示“相同值和相同类型”,而不是这种情况,字符串不会等于布尔值。

  • When displaying it with echo $client->getWatermarkInBackground(), the second line will return 1, because it's how the computer understands the value true.
  • 当使用echo $ client-> getWatermarkInBackground()显示它时,第二行将返回1,因为它是计算机如何理解值true。

Which one to choose? Well, speak to the computer in its language, it's better, mostly for the programmer who will look at your code (or yourself): if you use 'True', I understand you want to display the string, if you use true, I understand you want to use it in some test later on.

哪一个选择?好吧,用它的语言与计算机对话,它更好,主要是为了看你的代码(或你自己)的程序员:如果你使用'True',我理解你要显示字符串,如果你使用true,我明白你想在以后的某些测试中使用它。

#4


0  

Most functions you will see in PHP are smart enough to parse strings; therefore, when you pass "True" to a function, it will parse it and evaluate it as a boolean value, true.

您将在PHP中看到的大多数函数都足够智能来解析字符串;因此,当您将“True”传递给函数时,它将解析它并将其评估为布尔值true。

However, this is bad practice and can cause some errors.

但是,这是不好的做法,可能会导致一些错误。

If you want to be correct in all cases, do not use any quotes.

如果您想在所有情况下都正确,请不要使用任何引号。

true --> boolean value
"true" --> string

Edit

According to php.net,

根据php.net,

var_dump((bool) "false");   // bool(true)

Casting from "false" to a boolean value will give you a true boolean value, therefore, you must be very careful; remember that the only thing that will cast to a boolean value false is the number 0 or an empty object.

从“false”转换为布尔值将为您提供一个真正的布尔值,因此,您必须非常小心;请记住,唯一会转换为布尔值false的是数字0或空对象。

Therefore, in all cases where possible, you should use true, with no quotes.

因此,在可能的所有情况下,您应该使用true,不带引号。

#5


0  

Any string will always evaluate to true (i.e. a string is a non zero value). Therefore "false" is also true, strictly speaking. Essentially, by using "true", you are evaluating to true almost by luck.

任何字符串将始终求值为true(即字符串为非零值)。因此,严格来说,“虚假”也是如此。从本质上讲,通过使用“true”,你几乎可以通过运气评估为真。

Stick with

function(true) and function(false)

函数(true)和函数(false)

Instead of

function("true") and function("false")

function(“true”)和function(“false”)

#1


0  

$client->setWatermarkInBackground('True');

You're setting the value true as a string and not the correct Boolean.

您将值设置为字符串而不是正确的布尔值。

You should set it like this (2nd method) as to set boolean ( 0/1 ).

你应该像这样(第二种方法)设置它来设置布尔值(0/1)。

$client->setWatermarkInBackground(True);

Allowing you to do something like:

允许你做类似的事情:

if($client->setWatermarkInBackground()) {
    // is true do whatever you need?
}

#2


0  

There is no difference, since PHP promotes the undefined name True to the string "True". You should instead be using the lowercase literal true

没有区别,因为PHP将未定义的名称True提升为字符串“True”。您应该使用小写字面值true

#3


0  

'True' is the string True, in english.
True is the boolean value that means true for the computer.

'True'是字符串True,用英语表示。 True是布尔值,对计算机来说意味着true。

Although PHP is very good and understands both as the computer value, there are some differences:

虽然PHP非常好并且将它们理解为计算机价值,但是存在一些差异:

  • When using the test if ($client->getWatermarkInBackground() === true), the first line will return false, because the triple = means "Same value and same type", which is not the case, a string doesn't equal a boolean.
  • 当使用测试if($ client-> getWatermarkInBackground()=== true)时,第一行将返回false,因为triple =表示“相同值和相同类型”,而不是这种情况,字符串不会等于布尔值。

  • When displaying it with echo $client->getWatermarkInBackground(), the second line will return 1, because it's how the computer understands the value true.
  • 当使用echo $ client-> getWatermarkInBackground()显示它时,第二行将返回1,因为它是计算机如何理解值true。

Which one to choose? Well, speak to the computer in its language, it's better, mostly for the programmer who will look at your code (or yourself): if you use 'True', I understand you want to display the string, if you use true, I understand you want to use it in some test later on.

哪一个选择?好吧,用它的语言与计算机对话,它更好,主要是为了看你的代码(或你自己)的程序员:如果你使用'True',我理解你要显示字符串,如果你使用true,我明白你想在以后的某些测试中使用它。

#4


0  

Most functions you will see in PHP are smart enough to parse strings; therefore, when you pass "True" to a function, it will parse it and evaluate it as a boolean value, true.

您将在PHP中看到的大多数函数都足够智能来解析字符串;因此,当您将“True”传递给函数时,它将解析它并将其评估为布尔值true。

However, this is bad practice and can cause some errors.

但是,这是不好的做法,可能会导致一些错误。

If you want to be correct in all cases, do not use any quotes.

如果您想在所有情况下都正确,请不要使用任何引号。

true --> boolean value
"true" --> string

Edit

According to php.net,

根据php.net,

var_dump((bool) "false");   // bool(true)

Casting from "false" to a boolean value will give you a true boolean value, therefore, you must be very careful; remember that the only thing that will cast to a boolean value false is the number 0 or an empty object.

从“false”转换为布尔值将为您提供一个真正的布尔值,因此,您必须非常小心;请记住,唯一会转换为布尔值false的是数字0或空对象。

Therefore, in all cases where possible, you should use true, with no quotes.

因此,在可能的所有情况下,您应该使用true,不带引号。

#5


0  

Any string will always evaluate to true (i.e. a string is a non zero value). Therefore "false" is also true, strictly speaking. Essentially, by using "true", you are evaluating to true almost by luck.

任何字符串将始终求值为true(即字符串为非零值)。因此,严格来说,“虚假”也是如此。从本质上讲,通过使用“true”,你几乎可以通过运气评估为真。

Stick with

function(true) and function(false)

函数(true)和函数(false)

Instead of

function("true") and function("false")

function(“true”)和function(“false”)