I need some explanation on global $post [duplicate]

The global $post; command acts similar to the_post() function, but outside the loop.

When you want to use post’s data such as get_the_ID() or the_title() and you are not in the loop, you can use the global $post; to access these data. After so, you can have access to them, such as $post->ID or $post->post_title. This way, you can use the above functions as follows:

get_the_title( $post->ID );

In a loop, you can use the the_post() function to do this:

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        // Now you can use the_ID(); for example
    }
}