Skip to content

Add str starts with #2572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 28, 2023
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
Title: 'str_starts_with()'
Description: 'Performs a case-sensitive search to check if a string starts with a given substring.'
Subjects:
- 'Web Development'
- 'Web Design'
- 'Computer Science'
Tags:
- 'Strings'
- 'Functions'
CatalogContent:
- 'learn-php'
- 'paths/computer-science'
---

The **`str_starts_with()`** function is used to perform a case-sensitive search to check whether a string starts with a given substring.

## Syntax

```pseudo
$stringstartswith = str_starts_with($str, $substring);
```

The `$str` represents the input string to check.

The `$substr` represents the substring to search for in the input string.

The `str_starts_with()` function returns true if the `$str` starts with the `$substr` or false otherwise.

## Example

The example below demonstrates `str_starts_with()` operating on the same string in three ways (single character, multiple characters, and a case-sensitive example).

```php
<?php
$str = 'Peanut Butter Jelly';
$substr = 'P';
$stringstartswith = str_starts_with($str, $substr) ? 'begins' : 'does not begin';

echo ("the string $str $stringstartswith with $substr.\n");

$str = 'Peanut Butter Jelly';
$substr = 'Peanut';
$stringstartswith = str_starts_with($str, $substr) ? 'begins' : 'does not begin';

echo "the string $str $stringstartswith with $substr.\n";

$str = 'Peanut Butter Jelly';
$substr = 'p';
$stringstartswith = str_starts_with($str, $substr) ? 'begins' : 'does not begin';

echo "the string $str $stringstartswith with $substr.\n";
?>
```

This will result in the following output:

```shell
the string Peanut Butter Jelly begins with P.
the string Peanut Butter Jelly begins with Peanut.
the string Peanut Butter Jelly does not begin with p.
```