Skip to content
PHP Logo

How can you check if a string starts with a "/" in PHP?

To check if a string starts with a "/" in PHP, you can use the substr() function to extract the first character of the string and then compare it with "/". If the first character is "/", then the string starts with "/".

php
$string = "/example";
if (substr($string, 0, 1) === "/") {
    echo "The string starts with a '/'";
} else {
    echo "The string does not start with a '/'";
}