Well, this can be simply done by using the GLOBAL variable named as $_SERVER.
The $_SERVER is an array which has the attribute name REMOTE_ADDR.
Just assign it like this:
$userIp = $_SERVER['REMOTE_ADDR'];
Or use it directly like echo $_SERVER['REMOTE_ADDR'];
or echo ($_SERVER['REMOTE_ADDR']);.