How can PHP scripts differentiate between initial and subsequent calls when generating different pages using the same shortcode?

When generating different pages using the same shortcode in PHP, you can differentiate between initial and subsequent calls by setting a flag or variable to track the state of the shortcode execution. This flag can be checked at the beginning of the shortcode function to determine if it is the initial call or a subsequent call. By doing this, you can customize the output of the shortcode based on whether it is the first time it is being called or not.

// Flag to track initial call
$first_call = true;

// Shortcode function
function custom_shortcode_function( $atts ) {
    global $first_call;

    // Check if it is the initial call
    if ( $first_call ) {
        $output = "This is the initial call.";
        $first_call = false;
    } else {
        $output = "This is a subsequent call.";
    }

    return $output;
}
add_shortcode( 'custom_shortcode', 'custom_shortcode_function' );