A string is arbitrary text, such as “Hello World” or ‘single-quote example’.

String concatenation

Use the concatenation operator (period, .) to combine two strings. Example:

<?php
$txt="Hello";
$txt="World";
print $txt . " " . $txt2;   // outputs "Hello World!"
?>

The strlen() function

Use the strlen function to find the number of characters in a string.

<?php
echo strlen("Hello world!");  // outputs 12
?>

strlen() is one of many functions built into PHP. You can also create your own functions to perform specialized tasks.

The strpos() function

Use the strpos() function to search for a sub-string within a string.

If a match is found, strpos() returns the character position of the first match.

If no match is found, strpos() returns false.

Example:

<?php
echo strpos("Hello world!","world");   // outputs 6
?>

The position of the string “world” in the example above is 6 (and not 7) because strpos() uses zero-based counting.

strtoupper() and strtolower()

These functions make a string uppercase and lowercase:
Example:

<?php
echo strtoupper("Hello world!");   // outputs "HELLO WORLD!"
?>

Converting to all-uppercase or all-lowercase can be useful when comparing strings.

See Also

PHP String Functions @ w3schools