Zend certified PHP/Magento developer

How to add simple php or java function result into page or block?

How to add simple php or java function result into page or block?
I am writing a description of services provided. It is related to CGNAT. Would be good for reader to be displayed their external IP address. That could be done with simple function php code like:

<?php
//whether ip is from share internet
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
//whether ip is from proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//whether ip is from remote address
else
{
$ip_address = $_SERVER['REMOTE_ADDR'];
}
echo 'Your IP address is ';
echo  $ip_address;
if (str_starts_with ($ip_address, '100'))
{
echo '. It starts with 100, that means you are behind CGNAT.';
}
else
echo '. It does not appear like starts with 100. To be sure, you can logon into your 
router and check internet connection status. ';
?>

It is working if a php file placed in pub directory and called directly, but not working if the code itself or attempt to call the file placed inside page or block, like:

<?php include 'ipaddress.php';?>

How it supposed to be done correctly?
Also would appreciate more reliable code for spotting CGNAT.