webref.eu Posted July 29, 2008 Share Posted July 29, 2008 Hi All I'm using the two functions given below on the same page of code. You'll notice that they both contain the same variable, i.e. $result. I think this is fine, as the scope of the variable will be limited to within the function. Would you agree? I just want to check I'm not going to cause myself any headaches. Thanks all. //CODE STARTS function IsValidUsername($Username) { $result = TRUE; //ereg() searches a string for matches to the regular expression given in pattern in a case-sensitive way. //in regular expression, ^ means anything that is not if(ereg('[^A-Za-z0-9]', $Username)) { $result = FALSE; } return $result; } function IsValidPassword($Password) { $result = TRUE; //ereg() searches a string for matches to the regular expression given in pattern in a case-sensitive way. //in regular expression, ^ means anything that is not if(ereg('[^A-Za-z0-9]', $Password)) { $result = FALSE; } return $result; } //CODE ENDS Quote Link to comment https://forums.phpfreaks.com/topic/117213-is-using-the-same-variable-name-within-two-different-functions-ok/ Share on other sites More sharing options...
wildteen88 Posted July 29, 2008 Share Posted July 29, 2008 yes that is fine. Functions have their own variable scope. Quote Link to comment https://forums.phpfreaks.com/topic/117213-is-using-the-same-variable-name-within-two-different-functions-ok/#findComment-602938 Share on other sites More sharing options...
.josh Posted July 29, 2008 Share Posted July 29, 2008 Although I would recommend using just one function called IsValid(), seeing as how both of those functions do the exact same thing... Quote Link to comment https://forums.phpfreaks.com/topic/117213-is-using-the-same-variable-name-within-two-different-functions-ok/#findComment-602955 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.