好的,所以我運行了一個查詢,其中一行具有文本值而不是數字。所以當我嘗試運行使用未定義的常量(TEXT)
if($i['row'] = text)
{
echo "info here";
}
else
{
echo "other info here";
}
它返回使用未定義的常量。
那麼我怎麼寫if語句呢?
好的,所以我運行了一個查詢,其中一行具有文本值而不是數字。所以當我嘗試運行使用未定義的常量(TEXT)
if($i['row'] = text)
{
echo "info here";
}
else
{
echo "other info here";
}
它返回使用未定義的常量。
那麼我怎麼寫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";
}
爲,就必須使用純文本,PHP是考慮到它作爲一個常數。
相反,使用此代碼。
if ($i['row'] == 'text') {
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"; } ?>
我希望這回答你的問題