dongzhuang6247 2015-09-21 06:37
浏览 332
已采纳

StrPos总是返回False?

I've been working on a very basic search engine. It basically operates by checking if the word exists. If it does, it returns the link. I know most of you would suggest to create a database from phpMyAdmin but I don't remember the password to make the mySql_Connect command work.

Anyway here is the code:

<?php

session_start();

$searchInput = $_POST['search'];

var_dump($inputPage1);
var_dump($searchİnput);

$inputPage1 = $_SESSION['pOneText'];
$inputPage2 = isset($_SESSION['pTwoText']) ? $_SESSION['pTwoText'] : "";
$inputPage3 = isset($_SESSION['pThreeText']) ? $_SESSION['pThreeText'] : "";

if (strpos($inputPage1, $searchInput)) {
    echo "True";
} else {
    echo "False";
}

?>

When I search a word, any word from any page, weather it exists or not, it always returns false. Does anyone know why?

  • 写回答

3条回答 默认 最新

  • dongqun5769 2015-09-21 06:43
    关注

    From the PHP documentation:

    Warning: This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

    So the function returns the integer 0 since $searchInput starts at the first character of $inputPage1. Since it is inside an if condition, that expects a boolean, the integer is then converted to one. When converted to boolean, zero is equal to false so instead the else block is executed.

    To fix it, you need to use the !== operator (the not equal equivalent of ===):

    if (strpos($inputPage1, $searchInput) !== false) {
        //...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!