Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use str_contains over strpos for tutorial example #2716

Merged
merged 1 commit into from
Aug 28, 2023

Conversation

timacdonald
Copy link
Contributor

@timacdonald timacdonald commented Aug 27, 2023

The first example on the PHP documentation that isn't a "hello world" is searching the User-Agent for the string "Firefox".

This example currently uses strpos. I believe we should be using str_contains.

I believe this for a few reasons, but to start with I'll let the function descriptions do the talking:

Find the position of the first occurrence of a substring in a string

vs.

Determine if a string contains a given substring

str_contains matches the semantics of what we are doing and eases the overhead of understanding what is happening.

When using strpos we are also introducing the reader to:

  1. "Truthiness"
  2. Boolean literals
  3. PHP's "return false as a flag to indicate something" behaviour

Which is all additional overhead.

The current example also has double negatives that are hare to read left to right. The str_contains example feels much easier to comprehend, i.e.,

If strpos is not equal to false

versus

If str_contains

and...

returned non-false

vs

returned true

I understand that str_contain is a "newer" PHP function, however these are the documentation for the current PHP language - so we should take advantage and promote it's newer features, when it makes sense.

The str_contains function is available in all currently supported PHP versions.

Lastly, I removed the <br /> from the example as it serves no purpose to introducing the reader to "control structures and functions". The next example handles mixing in HTML in the "Mixing both HTML and PHP modes". The later examples also don't reference the <br /> anyway, so kinda feels like unneeded noise we ca drop.

@timacdonald timacdonald marked this pull request as ready for review August 27, 2023 23:36
?>
<h3>strpos() must have returned non-false</h3>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of "must have" feels like there is a possibility that we are here and it didn't return true or non-false.

Question: "Did Frank clean the car?"

Answer: "Must have".

Narrator: "It was actually the rain the cleaned the car".

@TimWolla
Copy link
Member

I understand that str_contain is a "newer" PHP function, however these are the documentation for the current PHP language - so we should take advantage and promote it's newer features, when it makes sense.

Agreed. This change LGTM, thank you!

@TimWolla TimWolla merged commit 60bd8b7 into php:master Aug 28, 2023
2 checks passed
@timacdonald timacdonald deleted the str_contains branch September 2, 2023 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants