无论如何,要使下面的代码仍然使用一个开关,b而不返回它,是a吗?谢谢!
$var = 0;
switch($var) {
case NULL : return 'a'; break;
default : return 'b'; break;
}
当然,如果使用if语句,您将像这样:
$var = 0;
if($var === NULL) return 'a';
else return 'b';
但是对于更复杂的示例,这变得很冗长。
无论如何,要使下面的代码仍然使用一个开关,b而不返回它,是a吗?谢谢!
$var = 0;
switch($var) {
case NULL : return 'a'; break;
default : return 'b'; break;
}
当然,如果使用if语句,您将像这样:
$var = 0;
if($var === NULL) return 'a';
else return 'b';
但是对于更复杂的示例,这变得很冗长。
Answers:
抱歉,您不能===在switch语句中使用比较,因为根据switch()文档:
请注意,开关/外壳确实比较宽松。
这意味着您必须提出解决方法。从松散的比较表,你可以利用的事实NULL == "0"是类型转换错误:
<?php
$var = 0;
switch((string)$var)
{
case "" : echo 'a'; break; // This tests for NULL or empty string
default : echo 'b'; break; // Everything else, including zero
}
// Output: 'b'
?>
不使用switch-仅进行所谓的“松散”比较。您始终可以使用将其替换为一个if/else if块===。
switch(请参见我的答案)执行此操作,尽管我确实认为这if-else对特定工作更好。
break或return呢?
想必您正在打开变量并需要整数。为什么不简单地事先使用来检查变量的整数状态is_int($val) 呢?
php中的switch语句仅做宽松的比较(==)参见http://www.php.net/manual/zh/control-structures.switch.php
如果需要严格比较,请使用if / elseif / else。
这是不可能的。
你可以,但是,把if报表里面的switch:
switch($var) {
// Loose cases here
case 0:
if($var === NULL) {
return 'a';
}
// Fall through
default:
return 'b';
}
或者简单地:
switch($var) {
// Loose cases here
default:
if($var === NULL) {
return 'a';
}
return 'b';
}
switch有时是使用超出严格检查范围的原因。
您还可以打开变量的类型:
switch (gettype($var)) {
...
}
Never use gettype() to test for a certain type, since the returned string may be subject to change in a future version. In addition, it is slow too, as it involves string comparison. Instead, use the is_* functions.
最好的方法之一是通过使用来检查NULL值 is_null
<?php
echo getValue();
function getValue()
{
$var = 0;
switch(true) {
case is_null($var) : return 'a'; break;
default : return 'b'; break;
}
}
?>
=== null因为它避免了不必要的函数调用开销。
创建一个类似断言的类,并将所需的任何逻辑放入其中;只要返回“ true”方法$this(就没有其他方法可以避免false-positives。)
class Haystack
{
public $value;
public function __construct($value)
{
$this->value = $value;
}
public function isExactly($n)
{
if ($n === $this->value)
return $this;
}
}
$var = new Haystack(null);
switch ($var) {
case $var->isExactly(''):
echo "the value is an empty string";
break;
case $var->isExactly(null):
echo "the value is null";
break;
}
或者,您可以将开关放入实际的类中:
class Checker
{
public $value;
public function __construct($value)
{
$this->value = $value;
}
public function isExactly($n)
{
if ($n === $this->value)
return $this;
}
public function contains($n)
{
if (strpos($this->value, $n) !== false)
return $this;
}
public static function check($str)
{
$var = new self($str);
switch ($var) {
case $var->isExactly(''):
return "'$str' is an empty string";
case $var->isExactly(null):
return "the value is null";
case $var->contains('hello'):
return "'$str' contains hello";
default:
return "'$str' did not meet any of your requirements.";
}
}
}
var_dump(Checker::check('hello world')); # string(28) "'hello world' contains hello"
当然,到那时,您可能希望重新评估要检查的内容,并使用真实的验证库。
如果要测试变量的值和类型,然后构建一个包含这两种信息的新字符串变量,并将其与不同的方案(通过串联)进行比较,则在实现所有可能的类型(根据gettype( )文档),例如:
<?php
$var= 9999;
$valueAndTypeOfVar = (string)$var.' '.gettype($var);
switch($valueAndTypeOfVar) {
case "$var boolean" : echo 'a'; break;
case "$var integer" : echo 'b'; break;
case "$var double" : echo 'c'; break;
case "$var string" : echo 'd'; break;
case "$var array" : echo 'e'; break;
case "$var object" : echo 'f'; break;
case "$var resource" : echo 'g'; break;
case "$var NULL" : echo 'h'; break;
case "$var unknown type" : echo 'i'; break;
default: echo 'j'; break;
}
// Outputs: 'b'
?>