How can you extract the first 50 characters from a variable in PHP?

To extract the first 50 characters from a variable in PHP, you can use the substr() function. This function allows you to extract a substring from a string starting at a specified position and with a specified length. By passing the variable and 0 as the starting position and 50 as the length, you can extract the first 50 characters from the variable.

$originalString = "This is a sample string with more than 50 characters.";
$first50Characters = substr($originalString, 0, 50);

echo $first50Characters;