1
0
mirror of https://github.com/php/doc-en.git synced 2026-03-23 23:32:18 +01:00

use str_contains over strpos for tutorial example (#2716)

This commit is contained in:
Tim MacDonald
2023-08-28 17:27:37 +10:00
committed by GitHub
parent 5ccb446e8a
commit 60bd8b786b

View File

@@ -263,8 +263,8 @@ Mozilla/5.0 (Linux) Firefox/112.0
<programlisting role="php">
<![CDATA[
<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false) {
echo 'You are using Firefox.<br />';
if (str_contains($_SERVER['HTTP_USER_AGENT'], 'Firefox')) {
echo 'You are using Firefox.';
}
?>
]]>
@@ -274,7 +274,7 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false) {
</para>
<screen role="html">
<![CDATA[
You are using Firefox.<br />
You are using Firefox.
]]>
</screen>
</example>
@@ -289,14 +289,13 @@ You are using Firefox.<br />
Reference</link> part of the manual.
</para>
<para>
The second concept we introduced was the <function>strpos</function>
function call. <function>strpos</function> is a function built into
PHP which searches a string for another string. In this case we are
The second concept we introduced was the <function>str_contains</function>
function call. <function>str_contains</function> is a function built into
PHP which determines is given a string contains another string. In this case we are
looking for <literal>'Firefox'</literal> (so-called needle) inside
<varname>$_SERVER['HTTP_USER_AGENT']</varname> (so-called haystack). If
the needle is found inside the haystack, the function returns the position
of the needle relative to the start of the haystack. Otherwise, it
returns &false;. If it does not return &false;, the <link
the needle is found inside the haystack, the function returns true. Otherwise, it
returns &false;. If it returns &true;, the <link
linkend="control-structures.if">if</link> expression evaluates to &true;
and the code within its {braces} is executed. Otherwise, the code is not
run. Feel free to create similar examples,
@@ -319,14 +318,14 @@ You are using Firefox.<br />
<programlisting role="php">
<![CDATA[
<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false) {
if (str_contains($_SERVER['HTTP_USER_AGENT'], 'Firefox')) {
?>
<h3>strpos() must have returned non-false</h3>
<h3>str_contains() returned true</h3>
<p>You are using Firefox</p>
<?php
} else {
?>
<h3>strpos() must have returned false</h3>
<h3>str_contains() returned false</h3>
<p>You are not using Firefox</p>
<?php
}
@@ -338,7 +337,7 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false) {
</para>
<screen role="html">
<![CDATA[
<h3>strpos() must have returned non-false</h3>
<h3>str_contains() returned true</h3>
<p>You are using Firefox</p>
]]>
</screen>
@@ -349,7 +348,7 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false) {
of PHP mode and just sent straight HTML. The important and powerful point
to note here is that the logical flow of the script remains intact. Only
one of the HTML blocks will end up getting sent to the viewer depending on
the result of <function>strpos</function>. In other words, it depends on
the result of <function>str_contains</function>. In other words, it depends on
whether the string <literal>Firefox</literal> was found or not.
</para>
</section>