From 60bd8b786b9bce22104fb22073eb6ca6b9714945 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 28 Aug 2023 17:27:37 +1000 Subject: [PATCH] use `str_contains` over `strpos` for tutorial example (#2716) --- chapters/tutorial.xml | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/chapters/tutorial.xml b/chapters/tutorial.xml index 2904367694..9478f60b17 100644 --- a/chapters/tutorial.xml +++ b/chapters/tutorial.xml @@ -263,8 +263,8 @@ Mozilla/5.0 (Linux) Firefox/112.0 '; +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) { +You are using Firefox. ]]> @@ -289,14 +289,13 @@ You are using Firefox.
Reference part of the manual. - The second concept we introduced was the strpos - function call. strpos 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 str_contains + function call. str_contains is a function built into + PHP which determines is given a string contains another string. In this case we are looking for 'Firefox' (so-called needle) inside $_SERVER['HTTP_USER_AGENT'] (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 if 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.
-

strpos() must have returned non-false

+

str_contains() returned true

You are using Firefox

-

strpos() must have returned false

+

str_contains() returned false

You are not using Firefox

strpos() must have returned non-false +

str_contains() returned true

You are using Firefox

]]>
@@ -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 strpos. In other words, it depends on + the result of str_contains. In other words, it depends on whether the string Firefox was found or not.