How can PHP be used to limit the number of characters displayed in a string output?

To limit the number of characters displayed in a string output 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 the length of characters to extract. By specifying the maximum number of characters you want to display, you can easily limit the length of the output string.

$string = "This is a sample string.";
$max_characters = 10;

if(strlen($string) > $max_characters){
    $output = substr($string, 0, $max_characters) . "...";
} else {
    $output = $string;
}

echo $output;