PHP str_split() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, str_split() splits a string into an array of smaller strings of a fixed length.

By default each piece is one character. Pass a length to make bigger chunks.

Syntax

Call str_split() with the values you need. Start with this line.

PHP
$chars = str_split("PHP");
CallWhat it does
str_split("PHP")Returns ["P", "H", "P"].
str_split("abcdefgh", 3)Returns ["abc", "def", "gh"].
array_sum(str_split("12345"))Returns 15 (the sum of the digits).

The last chunk can be shorter than the length. For UTF-8 text, use mb_str_split() so characters are not cut in the middle. To split by a delimiter, use explode().

Library declarations
PHP
str_split(string $string, int $length = 1): array

Parameters

ParameterDescription
stringRequired. The string to split.
lengthOptional. The size of each piece. Default is 1. Must be at least 1.

Return Value

Return ValueDescription
arrayAn array of string chunks.

PHP Examples

Example 1 — Split into Characters

With no length, each character becomes an array item.

PHP
<?php
$chars = str_split("PHP");

print_r($chars);

How It Works

  1. Pass a short string to str_split.
  2. The default length is 1.
  3. Each character becomes one array element.
  4. print_r shows the array with its indexes.

Example 2 — Split into Chunks

Use a length to group characters. The last chunk can be shorter.

PHP
<?php
$parts = str_split("abcdefgh", 3);

print_r($parts);

How It Works

  1. The string has 8 characters.
  2. A length of 3 gives chunks of three letters.
  3. Two full chunks use 6 characters.
  4. The remaining 2 characters form a shorter last chunk.

Example 3 — Add the Digits of a Number

Split digits into an array and sum them.

PHP
<?php
$digits = str_split("12345");

echo array_sum($digits), PHP_EOL;

How It Works

  1. Split "12345" into five one-character strings.
  2. The array contains "1", "2", "3", "4", "5".
  3. array_sum converts the numeric strings and adds them.
  4. The total is 15.

Frequently Asked Questions

str_split() breaks a string into an array of pieces. Each piece has the length you choose, which is 1 by default.
str_split() cuts by fixed length. explode() cuts at a delimiter such as a comma or space.
Not safely, because it splits bytes. Use mb_str_split() for text with accents, emojis, or non-English letters.
Before PHP 8.2 it returns an array with one empty string. From PHP 8.2 it returns an empty array.
PHP 8 throws a ValueError. The length must be 1 or greater.

Next: str_starts_with()

str_starts_with() checks whether a string begins with a given piece of text.

str_starts_with() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful