PHP : Check if a string starts with a substring

In this tutorial learn how to Check if a string starts with a substring in php using strpos() method.

function str_starts_with(string $string, string $substr): bool
{
    return strpos($string, $substr) === 0;
}

TUTORIAL

<?php
/**
 * Check if a string starts with a substring
 *
 * @param string $string
 * @param string $substr
 * @return bool
 */
function str_starts_with(string $string, string $substr): bool
{
    return strpos($string, $substr) === 0;
}

var_dump( str_starts_with("This is test!", "This"));

OUTPUT DEMO

bool(true)

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...