PHP: Basic String Operations

By Xah Lee. Date:

Strings can be joined by the period “.” symbol.

$a = "this" . " that";
echo $a; // prints “this that”

Substring extraction is done using the function “substr”. The form is: “substr( $myStr, $startIndex, $length)”. String index starts with 0.

$a = "once upon a time";
echo substr($a,0,4); // prints “once”

Length of the string is “strlen()”.

$a = "once";
echo strlen($a); // prints 3