PHP stristr() Tutorial 🎯

beginner
19 min

PHP stristr() Tutorial 🎯

Welcome to our comprehensive guide on the PHP stristr() function! By the end of this tutorial, you'll be well-equipped to utilize this powerful tool in your PHP projects. πŸ“ Note: This tutorial is suitable for both beginners and intermediate learners.

Understanding stristr() πŸ’‘ Pro Tip:

The stristr() function in PHP is used to search a substring in a given string case-insensitively. It's a handy function when you need to find a pattern regardless of upper or lower case letters.

Basic Usage πŸ’‘ Pro Tip:

Let's dive right into an example:

php
<?php $str = "Welcome to CodeYourCraft!"; $needle = "craft"; $result = stristr($str, $needle); echo $result; ?>

In this example, we're searching for the string "craft" in the string "Welcome to CodeYourCraft!". The stristr() function returns the portion of the string that matches the needle. In this case, the output will be "craft".

stristr() Return Value πŸ’‘ Pro Tip:

The stristr() function returns the part of the string that matches the needle or FALSE if no match is found. Here's an example:

php
<?php $str = "Welcome to CodeYourCraft!"; $needle = "example"; $result = stristr($str, $needle); if ($result === FALSE) { echo "No match found."; } else { echo $result; } ?>

In this example, since the string "example" is not present in the given string, the output will be "No match found."

Advance Usage πŸ’‘ Pro Tip:

You can use stristr() in more complex scenarios as well. For instance, let's say you want to check if a user's email address contains the domain "example.com":

php
<?php $email = "user@example.org"; $needle = "@example.com"; if (stristr($email, $needle)) { echo "Email contains example.com."; } else { echo "Email does not contain example.com."; } ?>

In this example, the output will be "Email does not contain example.com." because the email address user@example.org doesn't contain the domain "example.com".

Quiz πŸ’‘ Pro Tip:

Quick Quiz
Question 1 of 1

What does the PHP `stristr()` function do?

We hope you enjoyed this tutorial on the PHP stristr() function. In our next lesson, we'll explore more PHP string functions to help you become a master of PHP programming! 🎯 Pro Tip: Stay tuned for our next tutorial on PHP strpos().

Happy coding! πŸš€