簡體   English   中英

我不知道php語法

[英]I can't figure out php syntax

我這里需要分類法,

if ( $the_tax->name == 'day' , 'drink_type') {
        $tax_output = '';
    }
    return $tax_output;
}

上面的代碼寫錯了,白天的分類法,我不知道如何引用Drink_type的第二個分類法。 我用逗號嗎? 或|| 或什么()如果有人知道php語法並可以提供幫助!

您需要邏輯OR (可以用|| (通用語法)編寫,也可以僅是OR (特定於PHP),所以我不建議您使用)-有關更多詳細信息, 請參閱邏輯運算符doc 。因此,您的代碼應如下所示:

if( ($the_tax->name == 'day') || ($the_tax->name == 'drink_type') ) {
   // at least one condition met
}

或使用in_array()來獲得更多允許的名稱:

$names = ['day','drink_type'];
if( in_array($the_tax->name, $names) {
  // allowed name...
}

||分隔條件 (邏輯或)

if ( $the_tax->name == 'day' ||  $the_tax->name == 'drink_type') {
        $tax_output = '';
}
return $tax_output;
}

根據您所說的,正確的語法為:

if ( $the_tax->name == 'day' || $the_tax->name == 'drink_type') { 
// Note that you can't use id ($var == 'X' || 'Y')

正確的語法在if語句的兩個條件之間使用兩個管道。 像這樣,

$tax_output;

if ( $the_tax->name == 'day' || $the_tax->name == 'drink_type') {
    $tax_output = '';
}
return $tax_output;

我省略了您的結尾}因為這樣會引發語法錯誤。 如果應該在該結尾處加上括號,則還應添加代碼中包含開頭括號的部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM