How to get country list array with their corresponding country codes I have used $countries = \Drupal\Core\Locale\CountryManager::getStandardList(); to render array of countries with country code as their keys. But when I debug the variable $countries; print_r($countries);die; it goes into infinite loop and result does not get rendered. It shows 404.

How to get country list array?
You get the infinite loop, because the country name is a translatable markup and print_r() can't handle this object. If you cast the country name to a string, then it is printable in php:

 $countries = \Drupal\Core\Locale\CountryManager::getStandardList();
 foreach ($countries as $key =&gt; $value) {
   $country_name = (string) $value;
   print "$key - $country_name\n";
 }


This is not needed for rendering, because twig can handle this object. This is only for debugging.