elmas156 Posted October 14, 2010 Share Posted October 14, 2010 I have a question about functions and what is the benefit of using a function over multiple if/else statements. For example: What would be the difference in using this function: <?php function getRealIpAddr() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet { $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } return $ip; } ?> or just using this code: <?php if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet { $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } ?> With the first example, I try to echo the ip address ($ip) to the page and nothing happens. With the second, I echo $ip and the ip address is shown on the page. Why? Am I doing something wrong or taking a shortcut that shouldn't be taken? This may seem like a silly question but I'm trying to figure this out and sometimes it's easier to just use multiple if/else statements to accomplish my task rather than an actual function. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 14, 2010 Share Posted October 14, 2010 If you're only using the chunk of code once, then making it into a function is not really worth it. If you're using the same chunk of code more than once and function is preferable. How are you using the function when the echo doesn't work. Ken Quote Link to comment Share on other sites More sharing options...
elmas156 Posted October 14, 2010 Author Share Posted October 14, 2010 Basically, I am recording a user's ip address when they use my site and take an action that inserts information into my database. When I use the function as shown and use $ip where I want the ip address displayed on the screen or inserted into the database, nothing happens. When I use the second example above and I use $ip to display or insert the ip address, everything works fine... not sure what I'm doing wrong. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 14, 2010 Share Posted October 14, 2010 To use the function, you need to do something like this: <?php function getRealIpAddr() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet { $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } return $ip; } $ip = getRealIpAddr(); echo $ip; ?> How are you using it? Ken Quote Link to comment Share on other sites More sharing options...
elmas156 Posted October 14, 2010 Author Share Posted October 14, 2010 that's it, I left out $ip = getRealIpAddr(); and I was trying to use just $ip before it was actually set as a variable outside of the function. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.