好的,所以我运行了一个查询,其中一行有文本值,而不是数字值。所以当我尝试运行时
if($i['row'] = text)
{
echo "info here";
}
else
{
echo "other info here";
}
它返回Use of undefined常量。
那么我该如何写if语句呢?
text
在这里是一个字符串。它应该在引号中。此外,如果您正在进行比较,则=
应为==
(为了进行严格比较,请使用===
)。
if($i['row'] == 'text')
或
if(gettype($i['row'] == 'string')
常数的使用不带引号。
define("SAMPLE_CONSTANT", 'sample value');
echo SAMPLE_CONSTANT; // prints sample value
因此text
在本文中被视为常数。从而给出了未定义常数的误差
尝试使用
if(is_string($i['row']))
{
echo "info here";
}
else
{
echo "other info here";
}
如果我正确理解您的问题,您需要将数组值与数据类型文本或数字进行比较。
下面这行使用了赋值运算符"="而不是比较运算符"==",我认为这是一个拼写错误。
if($i['row'] = text)
您需要使用php函数"is_string"来正确检查数据类型。所以你的代码应该变成下面的样子
<?php
if( true === is_string( $i['row'] ) ) {
echo "info here";
} else {
echo "other info here";
}
?>
您可能还想检查数据类型是否为数字或未使用函数"is_numeric",因此修改后的版本如下:
<?php
if( true === is_string( $i['row'] ) ) {
echo "info here";
} elseif( true === is_numeric( $i['row'] ) ) {
echo "other info here";
}
?>
我希望这能回答您的问题
由于您只使用了文本,php将其视为常量。
相反,请使用此代码。
if ($i['row'] == 'text') {
echo "info here";
} else {
echo "other info here";
}