Skip to main content

Command Palette

Search for a command to run...

Three New String Functions In PHP

Published
•1 min read•View as Markdown
Three New String Functions In PHP

Let’s talk about new string functions in php.

Photo by Ben Griffiths on Unsplash

str_contains()

<?php
$haystack = 'Twinkle Twinkle Little Star';
$needle = 'Star';

if(str_contains($haystack, $needle)){
// needle exists in haystack
}

str_contains("text string", ""); // true
str_contains("", ""); // true
str_contains("", "abc"); // false

str_starts_with()

<?php
$haystack = 'Twinkle Twinkle Little Star';
$needle = 'Twinkle Twin';

var_dump(str_starts_with($haystack, $needle));
// true

str_ends_with()

<?php
$haystack = 'Twinkle Twinkle Little Star';
$needle = 'le Star';

var_dump(str_ends_with($haystack, $needle));
// true

Hope you found this helpful!

Bonus: You must love my most read article : How To Use Single Responsibility Principle in PHP/Laravel
And most GitHub star repo:
https://github.com/mohasinhossain/SOLID-Principles-in-PHP

More from this blog

Untitled Publication

58 posts