PHP strpos未获得预期结果

PHP strpos未获得预期结果,php,strpos,Php,Strpos,使用strpos(),我没有得到想要的结果。我怀疑问题出在条件语句中。如果条件是真的,看起来一切都正常。但如果为false,则条件true的代码仍会执行。这是密码 <?php // require_once 'functions/functions.php'; ?> <?php if (isset($_POST['submit'])) { $string = $_POST['sentence']; $findString = $_POST['findstri

使用strpos(),我没有得到想要的结果。我怀疑问题出在条件语句中。如果条件是真的,看起来一切都正常。但如果为false,则条件true的代码仍会执行。这是密码

<?php
// require_once 'functions/functions.php';
?>

<?php

if (isset($_POST['submit'])) {
    $string = $_POST['sentence'];
    $findString = $_POST['findstring'];
    $strPosition = stripos($string, stringToFind($findString));

 //  if (($strPosition == true) || ($strPosition == 0)) {
if ($strPosition !== true) {  
    echo 'Found!', '<br><br>';
    echo 'In the string ', $string, '.', '<br>';
    echo 'And the word you want to find is ';
    $readStr = substr($string, $strPosition, strlen($findString));
    echo $readStr, '.', '<br>';

    if ($strPosition == 0) {
    echo 'It is at the beginning of the string.', '<br>';
}
else {
    echo 'It is in the ', $strPosition, ' ', 'position.', '<br>';
}
}
else {
    echo 'Not found. Try again.', '<br>';
}
}

function stringToFind($findString)
{
    return $findString = $findString;
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>String Position</title>
</head>
<body>

<h1>Finding a string and then read it</h1><br><br>

<form id="form1" class="form" action="<?php echo                         htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<label for="sentence">Sentence here:
<textarea id="sentence" name="sentence" value="Put a sentence here."></textarea></label>
Enter a string: <input type="text" name="findstring">
<input type="submit" name="submit" value="Go">
</form><br><br>
</body>
</html>

串位置
查找字符串,然后读取它


是,因为您的条件得到满足,即使返回false,因为您与0进行了松散比较。改变你下面的状况

if (($strPosition == true) || ($strPosition == 0)) {
if (($strPosition == true) || ($strPosition == 0)) {
有,


有帮助:

是,因为您的条件得到满足,即使返回false,因为您与0进行了松散的比较。改变你下面的状况

if (($strPosition == true) || ($strPosition == 0)) {
if (($strPosition == true) || ($strPosition == 0)) {
有,

帮助:来自php手册:

返回针相对于haystack>字符串开始的位置(与偏移无关)。还要注意,字符串位置从0开始,而不是从1开始。 如果未找到指针,则返回FALSE

如果条件允许,请更改此选项

if (($strPosition == true) || ($strPosition == 0)) {
if (($strPosition == true) || ($strPosition == 0)) {

从php手册:

返回针相对于haystack>字符串开始的位置(与偏移无关)。还要注意,字符串位置从0开始,而不是从1开始。 如果未找到指针,则返回FALSE

如果条件允许,请更改此选项

if (($strPosition == true) || ($strPosition == 0)) {
if (($strPosition == true) || ($strPosition == 0)) {


if($strPosition!==false)
应该是条件。
if($strPosition!==false)
应该是条件。老实说,我还是不完全明白!==但我听从了你的建议。不幸的是,问题仍然存在。即使答案为false,显示消息“not found”(未找到)的指令仍不显示。
var_dump($strPosition)&告诉我们它显示了什么。正在复制,var_dump()=布尔值为false。您是否说在使用我在回答中提供的条件进行更改后,它仍在运行?问题是在更改为!==,我没有改变代码块的真实状态。你的链接确实有帮助。现在我想我正在学习==和==之间的区别,我只需要调整代码中的一些语句,我相信我最终会得到我想要的。老实说,我仍然没有完全理解!==但我听从了你的建议。不幸的是,问题仍然存在。即使答案为false,显示消息“not found”(未找到)的指令仍不显示。
var_dump($strPosition)&告诉我们它显示了什么。正在复制,var_dump()=布尔值为false。您是否说在使用我在回答中提供的条件进行更改后,它仍在运行?问题是在更改为!==,我没有改变代码块的真实状态。你的链接确实有帮助。现在我想我正在学习==和===之间的区别,我只需要调整代码中的一些语句,我相信我最终会得到我想要的。