What' are all about str_contains function came with Php 8?

39 views   Php  2021.01.10


I heard about this new function str_contains in the release of php 8.0, Which is helpful as I think, but I don't understand the usage, Ans I'm wondering if it is used as contains() we have in javascript, Which existing function will str_contains replace?

Answers, ideas and Comments

» 1 idea

gadrawin • 1 week ago
Many years ago, Php developers only had few options to choose from when they want to perform a search for a needle inside a given string and two functions which are strstr and strpos although there were some tricks to use, those functions were hard to use and sometimes confusing to some php developers. To understand the usefulness of str_contains function, Let's compare these two examples below to find out new improvements.


// First way (with old functions)

$sentence = "Thanks to php 8 gave us a new function";
// keyword to find in our  sentence
$keyword = "function"; 

$position = strpos($sentence , $keyword);

if($pos !== false) { 
// remember this comparison operator is used to check 
// if 2 variables are of same type
    echo " Keyword is found";
} else {
    echo " Keyword is not found";
}



// Second way with str_contains

$sentence = "Thanks to php 8 gave us a new function";
$keyword = "function"; 

if(str_contains($sentence, $keyword)) { 
    echo " Keyword is found";
} else {
    echo " Keyword is not found";
}

 »

0 



In order to go deep, share any relevant idea or posting solution to some questions, You have to sign in or create a new account if you don't have one. And get Donnekted(connected)




Log in now  Join the Team

This site is for sharing topics, questions, answers, experiences or other contents related to different stuffs, especially technology. For existing users, They can make any topic(post) to discuss about in hope for ideas or any result from other people from different communities. In order to get involved, Get donnekted(connected) by Joining us here!

Posted under this group »
This Donnektish is about php programming from version 5, 7 and 8, and popular frameworks built on top of it. No sales promotion allowed, Tricks and Projects first


52 people joined this donnektish


Related posts/ questions


Maintain social distancing, to avoid Covid19