HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PHPVariablesData TypesOperatorsStringsArraysAssociative ArraysConditions (if / else)Loops (for / foreach / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
PHP›Strings

Strings

Length, case conversion, replacement, splitting (explode), and joining (implode).

Simple enough for a 5-year-old to understand

Length and case

Example code
1
<?php
Start.
DefinitionStart.
2
echo strlen("hello");
Length: 7
Output / InputLength: 7
3
echo "\n";
New line.
Output / InputNew line.
4
echo strtoupper("hello");
HELLO
Output / InputHELLO
DefinitionOutput / Input

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the word "hexlibrary" in UPPERCASE.

🎯 Expected output: HEXLIBRARY

Loading editor…

About this topic

String processing is PHP's strongest suit — the raison d'être of the language is to generate web pages, which means generating text. There are hundreds of string functions in the standard library.

In PHP, the quote type determines behavior: inside double quotes, variables are interpolated and escape sequences work, while inside single quotes, everything is taken literally. For long texts, there is the heredoc syntax, which is ideal for generating HTML blocks.

A critical point: PHP's classic string functions count BYTES, not characters. Since Turkish characters take up two bytes, strlen returns a larger result than expected. For multibyte strings, it is mandatory to use functions with the mb_ prefix.

What you will learn

  • the difference between single and double quotes
  • the distinction between strlen and mb_strlen
  • the usage of strtoupper, trim, and str_replace
  • slicing and searching with substr and strpos
  • splitting and joining with explode and implode
  • generating formatted output with sprintf

When to use?

When outputting text into HTML, using htmlspecialchars is MANDATORY: directly outputting user-provided data creates an XSS vulnerability. This is the most common security mistake in PHP.

Instead of concatenating text in database queries, prepared statements are used; string concatenation opens the door to SQL injection.

Common mistakes

Wrong

echo "<p>" . $_GET["name"] . "</p>";

Directly outputting user-provided data into HTML is an XSS vulnerability: an attacker can send a script tag and execute code on the page. Any external data printed to the output must be escaped with htmlspecialchars.

Correct

echo "<p>" . htmlspecialchars($_GET["name"] ?? "") . "</p>";

Wrong

$text = "Tree";
echo strlen($text);

strlen counts BYTES; since Turkish characters take up two bytes, it comes out as 6 instead of 4. For character count, mb_strlen must be used. The same issue applies to substr, strtoupper, and strrev.

Correct

$text = "Tree";
echo mb_strlen($text);

Wrong

$text = "hello world";
if (strpos($text, "hello")) {
    echo "found";
}

strpos returns the found position; when the text is AT THE VERY BEGINNING, it returns 0, and since 0 evaluates to falsy, the condition fails. One must explicitly compare the result with false or use str_contains.

Correct

$text = "hello world";
if (str_contains($text, "hello")) {
    echo "found";
}

Frequently asked questions

Why are there mb_ prefixed functions?

Because PHP's classic string functions assume that every character is one byte. Languages like Turkish, Arabic, and Chinese break this assumption. The mb_ prefixed versions are character-aware; using them is mandatory when working with Turkish content.

When is htmlspecialchars necessary?

EVERYWHERE you output a value coming from the user or the database into HTML output. No exceptions. Skipping this function means an XSS vulnerability and is one of the most common security flaws in web applications.

what is heredoc?

It is a way to write multi-line text without escaping quotes. Variables are interpolated inside it, and it is very well-suited for generating HTML blocks. If you don't want interpolation, there is the nowdoc format — the same structure but with single-quote behavior.

Summary

PHP is powerful in string processing, but classic functions count bytes — Turkish content requires mb_ prefixed versions. Any external data output to HTML must be escaped with htmlspecialchars, and strpos returning 0 is a classic trap.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code