strlen()
Try it yourself →Returns the number of characters.
Syntax
strlen($text)
Description
Returns the number of characters in the text.
Example
<?php
echo strlen("merhaba");Expected output
7
When to use?
Length check (passwords, limits).
Text length, transformation, splitting, and searching.
| Command | Description |
|---|---|
| strlen() | Returns the number of characters. |
| strtoupper() / strtolower() | Converts to uppercase / lowercase. |
| trim() | Removes leading/trailing whitespace. |
| str_replace() | Replaces a part with something else. |
| substr() | Extracts a part of the text. |
| explode() / implode() | Splits text into an array / joins an array into text. |
| strpos() | Finds the position of a substring. |
Returns the number of characters.
Syntax
strlen($text)
Description
Returns the number of characters in the text.
Example
<?php
echo strlen("merhaba");Expected output
7
When to use?
Length check (passwords, limits).
Converts to uppercase / lowercase.
Syntax
strtoupper($text)
Description
Returns new text; does not change the original.
Example
<?php
echo strtoupper("hexlibrary");Expected output
HEXLIBRARY
When to use?
Ignoring case sensitivity in comparisons.
Removes leading/trailing whitespace.
Syntax
trim($text)
Description
Cleans up spaces/newlines at the beginning and end of the text.
Example
<?php
echo trim(" space ");Expected output
space
When to use?
Cleaning form input.
Replaces a part with something else.
Syntax
str_replace($old, $new, $text)
Description
Replaces all old parts in the text with the new one.
Example
<?php
echo str_replace("leo", "emma", "leo max leo");Expected output
emma max emma
When to use?
Text cleaning, filling templates.
Extracts a part of the text.
Syntax
substr($text, $start, $length)
Description
Returns a substring starting from baslangic (with optional length). Negative baslangic counts from the end.
Example
<?php
echo substr("HexLibrary", 0, 3);Expected output
Hex
When to use?
First N characters, truncation.
Splits text into an array / joins an array into text.
Syntax
explode($separator, $text) · implode($separator, $arr)
Description
explode splits text into parts (arr); implode does the reverse (text).
Example
<?php
$parts = explode(",", "a,b,c");
echo implode("-", $parts);Expected output
a-b-c
When to use?
CSV parsing, arr→text conversion.
Finds the position of a substring.
Syntax
strpos($text, $part)
Description
Returns the starting position of the part (from 0); returns false if not found.
Example
<?php
echo strpos("hello", "ll");Expected output
2
When to use?
Searching, checking if a word exists.
Common mistakes