如何在PHP功能下中断(如果)

时间:2019-01-14 11:09:20

标签: php

我是初学者,学习在php上进行编码,我创建了简单的代码以确保输入的内容不是整数。我该怎么办?

<?php 

function paint($room="office",$color="red")
{
if ($room=integer)
    {
    echo "Cannot input number"; break;
    }
else;
return "the color of the {$room} is {$color}.<br />";
}

echo paint();
echo paint("bedroom","blue");
echo paint("kitchen",null);
echo paint(2,4);  //i want to give message "cannot input number"
?>

4 个答案:

答案 0 :(得分:1)

如果要退出功能,只需使用return;语句。但是,就您而言,您只需将代码添加到if语句的else部分中即可。

if (is_numeric($room)) {
    echo "Cannot input number"; break;
} else {
    return "the color of the {$room} is {$color}.<br />";
}

或带有return语句:

function paint($room = "office", $color = "red")
{
    if (is_numeric($room)) {
        echo "Cannot input number";
        return;
    }

    return "the color of the {$room} is {$color}.<br />";
}

更新
如果您希望代码执行停止并显示一条消息,则最好的办法是引发异常。

function paint($room = "office", $color = "red")
{
    if (is_numeric($room)) {
        throw new Exception("Cannot input number");
    }

    return "the color of the {$room} is {$color}.<br />";
}

答案 1 :(得分:1)

以上大多数答案都可以带您找到解决方案。 所以这是另一种答案。

最佳做法:尝试与类型保持一致。

考虑函数应该返回什么。 在您的情况下,您希望返回string

最简单的解决方案是:

<?php

function paint($room="office",$color="red")
{
    if (is_numeric($room))
    {
        // It is an integer (e.g. 1)
        // or it is a float (e.g. 3.141)
        return "\ncannot input number");
    }

    if ($room === null) {
        // You may have to check for null, too
        return "\ncannot input null");
    }

    // or potentially any other "Type" a user can pass in.

    return "\nthe color of the {$room} is {$color}.<br />";
}

if条件内部的返回称为“早期返回”,可以帮助您使代码保持简单。

您现在总是返回一个string

在较新的PHP版本中,可以使用“类型系统”。 您想强制您的函数只接受字符串参数。

可以这样表示:

<?php 

function paint(string $room="office",string $color="red"): string {
    return "\nthe color of the {$room} is {$color}.<br />";
}

echo paint() // => the color of the office is red.<br />
echo paint(1) // => the color of the 1 is red.<br />

在这里看到“类型”吗? 此刻会产生一些奇怪的行为。 paint(1)“有效”。

这是因为php将1“投射”到"1"。在这种情况下,我们不希望这样做。

您可以在declare(strict_types=1);文件的开头中添加.php

<?php

declare(strict_types=1);

function paint(string $room="office",string $color="red"): string
{
    return "\nthe color of the {$room} is {$color}.<br />";
}


echo paint(); // => the color of the office is red.<br />
echo paint(1); // => Uncaught TypeError: Argument 1 passed to paint() must be of the type string, integer given

您现在将获得明确的错误消息,并且必须将字符串传递给该函数。 这很容易理解,您删除了许多边缘情况。例如。 is_int1.22float)不兼容。

很抱歉,答案很长:-) 但是我想向您展示一个“更现代/更强大”的解决方案。

答案 2 :(得分:0)

在上面的程序中,您使用了 break 语句而没有任何循环或switch语句,并且检查了变量是否为整数,您是否使用了integer这是错误的。使用函数is_int(<varname>)返回boolean

PHP解析器抛出错误:

PHP Fatal error:  'break' not in the 'loop' or 'switch' context in /home/main.php on line 7

您可以使用trigger_error("\ncannot input number", E_USER_ERROR);

尝试以下代码:

<?php 

function paint($room="office",$color="red")
{
if (is_int($room))
    {
   trigger_error("\ncannot input number", E_USER_ERROR);
    }
else{
return "\nthe color of the {$room} is {$color}.<br />";
}
}

echo paint();
echo paint("bedroom","blue");
echo paint("kitchen",null);
echo paint(2,4);  //i want to give message "cannot input number"
?>

答案 3 :(得分:0)

您的代码有几个问题。

  • $var = integer是分配,而不是比较。由于integer既不是保留字,也不是定义的常量,因此它将转换为字符串'integer'并发出警告。在以后的PHP版本中,这将被视为致命错误。使用相同的运算符 ===进行比较。

  • 您无法通过$var === typename检查类型。有实现此目的的功能。 is_int($var)检查变量是否为整数类型。

  • break语句只能在循环和切换结构中使用。

  • else; return "";没有条件返回。 else分支为空。您可以使用没有代码块的if-else-constructs。下一条语句有条件地执行。如果是一个块,则该块中的所有内容都是有条件的。如果它是分号;,那么它实际上是一个空语句。

这是您翻译的函数:

function paint($room = 'office', $color = 'red')
{
  // always true since non-empty strings evaluate to true in boolean context 
  if ($room = 'integer')
  {
    echo "Cannot input number"; /* break; is illegal here */
  }
  else
  {

  }

  // this is executed always
  return "the color of the {$room} is {$color}.<br />";
}

您可能想做:

function paint($room = 'office', $color = 'red')
{
  if (is_int($room))
  {
    echo "Cannot input number";
    return '';
  }

  return "the color of the {$room} is {$color}.<br />";
}

问题在于,整数不是实际问题,而是复杂的数据类型,不提供适当的字符串表示形式。您应该改用类型提示。

declare( strict_types = 1);

function paint(string $room = 'office', string $color = 'red') : string
{
  return "the color of the {$room} is {$color}.<br />";
}

如果您声明strict_types,则在函数之间传递参数不会隐式更改类型。