如何通过php代码获取URL中的本地IP

I need a php related help for moodle config file. I install xampp on usb flash drive, then I install a moodle version in it. Now I wish to run this in my lan with dynamic ip address, like I put my flash drive in any system of my lan and start xampp server then it automatically take that system's ip address in url. This is my config file:

unset($CFG);
global $CFG;
$CFG = new stdClass();

$CFG->dbtype    = 'mysqli';
$CFG->dblibrary = 'native';
$CFG->dbhost    = 'localhost';
$CFG->dbname    = 'exam';
$CFG->dbuser    = 'root';
$CFG->dbpass    = '';
$CFG->prefix    = 'mdl_';
$CFG->dboptions = array (
'dbpersist' => 0,
'dbport' => '',
'dbsocket' => '',
);
$CFG->wwwroot   = "http://localhost/examination";
$CFG->dataroot  = 'F:\\xampp\\moodledata';
$CFG->admin     = 'admin';

$CFG->directorypermissions = 0777;

require_once(dirname(__FILE__) . '/lib/setup.php');

// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!

Now in line "$CFG->wwwroot = "http://localhost/examination";" I wish to put ip address dynamically in place of localhost and for this I found a function:

// function to get ip addresss
function getLocalIP(){
exec("ipconfig /all", $output);
foreach($output as $line){
if (preg_match("/(.*)IPv4 Address(.*)/", $line)){
$ip = $line;
$ip = str_replace("IPv4 Address. . . . . . . . . . . :","",$ip);
$ip = str_replace("(Preferred)","",$ip);
}
}
return $ip;
}
//echo $ip = getLocalIP();

But when I put above function in line like $CFG->wwwroot = "http://$ip/examination"; it wont work. Can anyone help me in this??

You should call function name getLocalIP() instead of $ip .