How can you extract only the first three characters of a variable in PHP?

To extract only the first three characters of a variable in PHP, you can use the substr() function. This function allows you to extract a substring from a string based on the starting position and length. By specifying the starting position as 0 and the length as 3, you can extract the first three characters of the variable.

$originalString = "Hello World";
$firstThreeChars = substr($originalString, 0, 3);

echo $firstThreeChars; // Output: Hel