【问题标题】:How i define "in" in php something like in python? [duplicate]我如何在 php 中定义“in”,就像在 python 中一样? [复制]
【发布时间】:2022-07-07 20:30:11
【问题描述】:

可以在php中做"in"吗? python 中的类似内容:

test = ["Hi", "Hello", "Good morning"]
msg = input("Hello!\n")

if msg in test:
  print("Hello, you are a good boy!")
else:
  print("Try it again please!")

【问题讨论】:

  • PHP 没有 in 运算符,您需要例如in_array.

标签: python php


【解决方案1】:

像这样使用in_array怎么样:

$message = 'Hello';
if (in_array($message, ["Hi", "Hello", "Good morning"])){
    // True
}

【讨论】:

    【解决方案2】:

    我不懂python,但这听起来像in_array 函数:

    $test = ["Hi", "Hello", "Good morning"];
    $msg = "Hello!\n";
    
    if(in_array($msg, $test) === true){
      echo "Hello, you are a good boy!"
    }else{
      "Try it again please!"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 2010-12-26
      相关资源
      最近更新 更多