简体   繁体   English

为什么 strpos 第一次工作,而不是第二次?

[英]Why is strpos working the first time, but not the second?

I´m using strpos two timer.我正在使用 strpos 两个计时器。 In the first if/else it works well but in the second it doesn´t work.在第一个 if/else 中它运行良好,但在第二个中它不起作用。 This is my code:这是我的代码:

if (strpos($word, "mono") == true) {
    $type = "Monobloc";
} else {
    $type = "Articulated";
}

if ($word, "galva") == true) {
    $coating = "Galvanized Rod";
} elseif (strpos($word, "epoxi") == true) {
    $coating = "EPOXI 100%";
} elseif ($word, "electro") == true) {
    $coating = "Electrozinced";
}

Example: If the variable word has the value "galva-mono" $type should be "Monobloc" and $coating should be "Galvanized Rod".示例:如果变量 word 的值为“galva-mono”,则 $type 应为“Monobloc”,而 $coating 应为“Galvanized Rod”。 The problem is that it is assigning well the $type but in the coating it doen´t enter in the if clause.问题是它很好地分配了 $type 但在涂层中它没有进入 if 子句。

As stated in the official documentation :官方文档所述

Warning警告

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.此函数可能返回布尔值 FALSE,但也可能返回计算结果为 FALSE 的非布尔值。 Please read the section on Booleans for more information.请阅读有关布尔值的部分以获取更多信息。 Use the === operator for testing the return value of this function.使用 === 运算符来测试此函数的返回值。

You are checking the result with == true instead of !== false .您正在使用== true而不是!== false检查结果。

So, try this code:所以,试试这个代码:

if (strpos($word, "mono") !== false) {
    $type = "Monobloc";
} else {
    $type = "Articulated";
}

if (strpos($word, "galva") !== false) {
    $coating = "Galvanized Rod";
} elseif (strpos($word, "epoxi") !== false) {
    $coating = "EPOXI 100%";
} elseif (strpos($word, "electro") !== false) {
    $coating = "Electrozinced";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM