What is the best way to output only a portion of a variable in PHP?

To output only a portion of a variable in PHP, you can use the substr() function. This function allows you to extract a specific portion of a string variable based on the starting position and length you specify. Simply pass the variable, the starting position, and the length of the desired portion to substr() to achieve this.

// Example variable
$fullString = "Hello, World!";

// Output only a portion of the variable
$portion = substr($fullString, 0, 5);
echo $portion; // Output: Hello