Example 1 — Check a URL Prefix
The everyday use: does a string begin with some text?
<?php
$url = "https://codetofun.com";
echo str_starts_with($url, "https://") ? "Secure link" : "Not secure", PHP_EOL;
echo str_starts_with($url, "http://") ? "Plain link" : "Not a plain link", PHP_EOL; How It Works
- Store the string. $url holds a full web address.
- Test the prefix. The first call checks for
"https://". - Get a boolean. The result is
true, so the ternary prints "Secure link". - Try another prefix.
"http://"does not match, so the result isfalse.

