<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Irrelevant code examples at the beginning of the document</span></span><span>
</span><span><span class="hljs-variable">$example</span></span><span> = </span><span><span class="hljs-string">"Example content"</span></span><span>;
</span><span><span class="hljs-meta">?></span></span><span>
<p><hr></p>
<p></span><?php<br>
/**</p>
<ul>
<li>
<p>How to check and correctly handle the situation when mb_strstr function returns false.</p>
</li>
<li></li>
<li>
<p>In PHP, the mb_strstr function is used to search for a substring within a multi-byte string and returns the substring along with the part following it.</p>
</li>
<li>
<p>The function signature is:</p>
</li>
<li>
<p>string|false mb_strstr(string $haystack, string $needle, bool $before_needle = false, ?string $encoding = null)</p>
</li>
<li></li>
<li>
<p>When the substring is not found, mb_strstr returns false. Therefore, special attention should be given to checking the return value</p>
</li>
<li>
<p>to avoid errors in subsequent operations.</p>
</li>
<li></li>
<li>
<p>Below is an example demonstrating how to check and handle when mb_strstr returns false:<br>
*/</p>
</li>
</ul>
<p>// Define a string containing multi-byte characters<br>
$haystack = "This is a test string used to demonstrate the usage of mb_strstr function.";<br>
$needle = "test";</p>
<p>// Call mb_strstr to search<br>
$result = mb_strstr($haystack, $needle, false, 'UTF-8');</p>
<p>if ($result === false) {<br>
// Substring not found, handle accordingly<br>
echo "Substring '{$needle}' not found.";<br>
} </span>else {<br>
// Substring found, proceed to handle the result<br>
echo "Found substring '{$needle}', result: " . </span>$result;<br>
}</p>
<p>/**</p>
<ul data-is-only-node="" data-is-last-node="">
<li>
<p>Notes:</p>
</li>
<li>
<ol>
<li>
<p>Use strict comparison (===) to check if the return value is false, to avoid misjudging empty strings and others.</p>
</li>
</ol>
</li>
<li>
<ol start="2">
<li>
<p>Specify the correct encoding parameter to ensure proper handling of multi-byte characters.</p>
</li>
</ol>
</li>
<li>
<ol start="3">
<li>
<p>If only the existence of the substring needs to be checked, mb_strpos may be a more appropriate choice.</p>
</li>
</ol>
</li>
<li></li>
<li>
<p>Summary:</p>
</li>
<li>
<p>If mb_strstr returns false, it means the substring was not found. By strictly checking the return value,</p>
</li>
<li data-is-last-node="">
<p data-is-last-node="">we can prevent errors in subsequent logic and ensure the stability of the program.<br>
*/<br>
?><br>
</span>