Skip to main content

PHP Strings

Strings are one of the most commonly used data types in PHP.

A string is a sequence of characters.

Examples:

$name = "Aayan";
$server = "ZyroNetwork";
$message = "Welcome to the server!";

Why Strings Matter

Strings are used everywhere:

✅ Player Names

✅ Chat Messages

✅ Commands

✅ Configurations

✅ JSON APIs

✅ Database Records

✅ Discord Bots

✅ PocketMine Plugins


Creating Strings

PHP provides two primary ways.


Double Quotes

$name = "Aayan";

Single Quotes

$name = 'Aayan';

Difference

Double quotes support variable interpolation.

Example:

$name = "Aayan";

echo "Hello $name";

Output:

Hello Aayan

Single quotes do NOT.

echo 'Hello $name';

Output:

Hello $name

String Length

Use:

strlen()

Example:

echo strlen("Zyro");

Output:

4

Unicode Strings

echo strlen("नमस्ते");

May produce unexpected results because PHP counts bytes.

Use:

mb_strlen()

Example:

echo mb_strlen("नमस्ते");

Concatenation

Strings can be joined using:

.

Example:

$name = "Aayan";

echo "Hello " . $name;

Output:

Hello Aayan

Concatenation Assignment

$message = "Hello";

$message .= " World";

Output:

Hello World

Escape Characters


New Line

echo "Hello\nWorld";

Tab

echo "Hello\tWorld";

Double Quote

echo "\"Hello\"";

Backslash

echo "\\";

Heredoc

Useful for large strings.

$text = <<<TEXT
Hello
World
TEXT;

Nowdoc

No interpolation.

$text = <<<'TEXT'
Hello $name
TEXT;

Accessing Characters

$name = "Aayan";

echo $name[0];

Output:

A

String Functions

PHP provides hundreds of functions.


strtoupper()

Converts to uppercase.

echo strtoupper(
"zyro"
);

Output:

ZYRO

strtolower()

echo strtolower(
"ZYRO"
);

Output:

zyro

ucfirst()

echo ucfirst(
"aayan"
);

Output:

Aayan

ucwords()

echo ucwords(
"zyro network"
);

Output:

Zyro Network

trim()

Removes spaces.

$name = "  Aayan ";

echo trim($name);

ltrim()

Removes left spaces.


rtrim()

Removes right spaces.


str_replace()

Replace text.

echo str_replace(
"Hello",
"Hi",
"Hello World"
);

Output:

Hi World

str_ireplace()

Case-insensitive replacement.


strpos()

Find first occurrence.

echo strpos(
"Hello World",
"World"
);

Output:

6

stripos()

Case-insensitive search.


str_contains()

PHP 8+

if (
str_contains(
$message,
"hello"
)
) {

}

str_starts_with()

str_starts_with(
"PocketMine",
"Pocket"
);

str_ends_with()

str_ends_with(
"plugin.yml",
".yml"
);

substr()

Extract portion of string.

echo substr(
"PocketMine",
0,
6
);

Output:

Pocket

explode()

Convert string into array.

$data =
explode(
",",
"A,B,C"
);

Output:

[
"A",
"B",
"C"
]

implode()

Convert array into string.

echo implode(
", ",
[
"A",
"B",
"C"
]
);

Output:

A, B, C

nl2br()

Convert new lines to HTML.


wordwrap()

Wrap long strings.


str_repeat()

echo str_repeat(
"=",
20
);

Output:

====================

str_pad()

echo str_pad(
"VIP",
10
);

number_format()

echo number_format(
1500000
);

Output:

1,500,000

HTML Functions


htmlspecialchars()

Extremely important for security.

echo htmlspecialchars(
"<script>"
);

Output:

&lt;script&gt;

htmlentities()

Converts special characters.


String Comparison


==

"abc" == "abc"

===

"abc" === "abc"

strcmp()

strcmp(
"abc",
"abd"
);

Returns:

-1
0
1

String Interpolation

$name = "Aayan";

echo "Hello {$name}";

Recommended syntax:

{$variable}

Variable Parsing

echo "Player: {$player->getName()}";

Multibyte Strings

For UTF-8 languages use:

mb_strlen()
mb_substr()
mb_strtoupper()

Install extension:

sudo apt install php-mbstring

Regular Expressions

PHP supports regex.


preg_match()

preg_match(
"/hello/",
"hello world"
);

preg_replace()

preg_replace(
"/[0-9]/",
"",
"abc123"
);

Output:

abc

preg_split()

Split using regex.


PocketMine Examples


Chat Formatting

$message =
"§6Zyro §8» §fHello";

Color Codes

str_replace(
"&",
"§",
$message
);

Command Parsing

$args =
explode(
" ",
$command
);

Config Values

$prefix =
$config->get(
"prefix"
);

JSON APIs

$json =
json_encode(
[
"name" => "Aayan"
]
);

Validation Examples


Username Length

if (
strlen($name) < 3
) {

}

Email Check

if (
str_contains(
$email,
"@"
)
) {

}

Permission Check

if (
str_starts_with(
$permission,
"zyro."
)
) {

}

Common Mistakes


if (
strpos(
$text,
"hello"
)
)

Fails if result is:

0

if (
strpos(
$text,
"hello"
) !== false
)

Forgetting UTF-8

strlen("नमस्ते")

mb_strlen("नमस्ते")

Concatenating Too Much

Avoid:

$a . $b . $c . $d . $e

Prefer:

sprintf()

Best Practices

✅ Use:

str_contains()

instead of:

strpos()

when possible.


✅ Escape HTML output.


✅ Use UTF-8.


✅ Prefer interpolation:

"Hello {$name}"

Exercises


Exercise 1

Convert:

zyronetwork

to uppercase.


Exercise 2

Split:

apple,banana,mango

using:

explode()

Exercise 3

Join array using:

implode()

Exercise 4

Replace:

&

with:

§

Mini Project

Create a chat formatter:

&6Admin:
Hello

Output:

§6Admin:
Hello

Challenge

Create:

formatMoney()
formatPlayerName()
formatRankPrefix()

Quiz

Which function returns string length?

Answer

strlen()


Which function converts string to array?

Answer

explode()


Which function joins arrays?

Answer

implode()


Which function checks substring?

Answer

str_contains()


Useful Links

PHP Strings:

https://www.php.net/manual/en/book.strings.php

String Functions:

https://www.php.net/manual/en/ref.strings.php

Regex:

https://www.php.net/manual/en/ref.pcre.php


Next Chapter

➡ PHP Arrays