PHP如果函数有多个结果[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我试图根据它所在的部门为属性返回不同的状态。有Sales和Lettings部门,每个部门都有一个表示属性状态的数字系统。

我试图使用if函数来查询每个属性的部门和状态,并为每个状态返回不同的文本字符串,但所有属性都标记为“保持”(即第一个选项)

代码如下。道歉并不是特别优雅。

function prop_status( $dept = null, $availability = null) {
if ( $dept = "Lettings" and $availability = "1") {
  return "On hold";
}
if ( $dept = "Lettings" and $availability = "2") {
  return "For rent";
}
if ( $dept = "Lettings" and $availability = "3") {
  return "References pending";
}
if ( $dept = "Lettings" and $availability = "4") {
  return "Let agreed";
}
if ( $dept = "Lettings" and $availability = "5") {
  return "Let";
}
if ( $dept = "Sales" and $availability = "1") {
  return "On hold";
}
if ( $dept = "Sales" and $availability = "2") {
  return "For sale";
}
if ( $dept = "Sales" and $availability = "3") {
  return "Under offer";
}
if ( $dept = "Sales" and $availability = "4") {
  return "Sold STC";
}
if ( $dept = "Sales" and $availability = "5") {
  return "Sold";
}
if ( $dept = "Sales" and $availability = "7") {
  return "Withdrawn";
}

任何有关使这项工作的帮助将非常感激。

php xml if-statement logic
1个回答
0
投票

你应该将所有=改为==

function prop_status( $dept = null, $availability = null) {
if ( $dept == "Lettings" and $availability == "1") {
  return "On hold";
}
if ( $dept == "Lettings" and $availability == "2") {
  return "For rent";
}
if ( $dept == "Lettings" and $availability == "3") {
  return "References pending";
}
if ( $dept == "Lettings" and $availability == "4") {
  return "Let agreed";
}
if ( $dept == "Lettings" and $availability == "5") {
  return "Let";
}
if ( $dept == "Sales" and $availability == "1") {
  return "On hold";
}
if ( $dept == "Sales" and $availability == "2") {
  return "For sale";
}
if ( $dept == "Sales" and $availability == "3") {
  return "Under offer";
}
if ( $dept == "Sales" and $availability == "4") {
  return "Sold STC";
}
if ( $dept == "Sales" and $availability == "5") {
  return "Sold";
}
if ( $dept == "Sales" and $availability == "7") {
  return "Withdrawn";
}

请参阅php here中的比较运算符

$a == $b如果$ a等于$ b,则返回TRUE

$a === $b如果$ a等于$ b,则返回TRUE,它们属于同一类型。

© www.soinside.com 2019 - 2024. All rights reserved.