dongzhuang6247 2015-09-20 22:37
浏览 333
已采纳

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-20 22: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部