kwdelre Posted June 15, 2011 Share Posted June 15, 2011 Hey everyone, I'm pretty inexperienced as my code may show. I am trying to clean up my code and organize it better by using a user-defined function. For some reason I can't get it to work though and I'm wondering if there is some code in my function that can't be (i.e. some MySql). I'm sure its something obvious to you all, I just haven't used my own functions before. The screen stays blank with no errors. Thanks for any help. Here is function I am defining. All code inside of the function works perfect until I put it in this function and call it. function CheckCertStatus ($host, $dbusername, $dbpassword, $TxQuizdb, $QuizResultsTable) { mysql_connect($host, $dbusername, $dbpassword); mysql_select_db($TxQuizdb); $QuizVerifyQuery = mysql_query("SELECT Score, Quiz_Name, User_Email, Quiz_Full_Name, Pass FROM ".$QuizResultsTable." WHERE User_Email = '".$_SESSION['Email']."' AND Pass = 'PASSED'"); while($QuizVerify = mysql_fetch_array($QuizVerifyQuery)) { if ($QuizVerify['Quiz_Name'] == "S1") { $S1 = 1; } if ($QuizVerify['Quiz_Name'] == "S2") { $S2 = 1; } if ($QuizVerify['Quiz_Name'] == "P1") { $P1 = 1; } if ($QuizVerify['Quiz_Name'] == "P2") { $P2 = 1; } if ($QuizVerify['Quiz_Name'] == "P3") { $P3 = 1; } if ($QuizVerify['Quiz_Name'] == "M1") { $M1 = 1; } } if (($S1 && $S2 && $P1 && $P2 && $P3 && $M1) == 1) { $Ready = 1; } } Calling it: CheckCertStatus(); Quote Link to comment https://forums.phpfreaks.com/topic/239383-user-defined-function-help/ Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 Well, you aren't passing any arguments to it when you call it. CheckCertStatus('localhost','root','root','quizes','results'); Something like that, only use your real credentials. Quote Link to comment https://forums.phpfreaks.com/topic/239383-user-defined-function-help/#findComment-1229823 Share on other sites More sharing options...
kwdelre Posted June 15, 2011 Author Share Posted June 15, 2011 Does it matter that some of these variables are from an "include ()" file? Quote Link to comment https://forums.phpfreaks.com/topic/239383-user-defined-function-help/#findComment-1229842 Share on other sites More sharing options...
fugix Posted June 15, 2011 Share Posted June 15, 2011 as the poster above said, you are receiving a blank page because you declare several parameters in your creation of your function, however when you call the function you do not pass any parameters to it... CheckCertStatus('$host','$username','$password','$quizes','$results'); as the poster above stated, is something of what you need, if you are including a file into the file where this function is being called, you will be able to use those variables as well as parameters in your function Quote Link to comment https://forums.phpfreaks.com/topic/239383-user-defined-function-help/#findComment-1229846 Share on other sites More sharing options...
KevinM1 Posted June 15, 2011 Share Posted June 15, 2011 Does it matter that some of these variables are from an "include ()" file? So long as they're in scope when you invoke your function, you'll be fine. --- Stepping back a moment, there seems to be some confusion on your part regarding both defining and invoking a function. So, hopefully, I'll be able to clarify some things for you in that regard. A function definition is the body of the function. So, something like: function exampleFunc($arg1) { // do things with $arg1 } is a definition. The function is not invoked at that point. It's simply a blueprint. Invoking a function is relatively simple. All you need to do is write out the function name, put parameters into its argument list (if any are needed), and place a semicolon at the end. Example: $x = 5; exampleFunc($x); In your case, you define a function with several expected parameters: function CheckCertStatus ($host, $dbusername, $dbpassword, $TxQuizdb, $QuizResultsTable) { // function definition } But you neglect to actually pass any parameters in when you actually try invoking your function: CheckCertStatus(); If you define your function to take parameters, you need to manually insert them when you actually invoke your function. So, you need to do: $hostName = 'localhost'; $user = 'me'; $pass = '******'; $quizDB = 'quizDB'; $DBTable = 'quiz'; CheckCertStatus($hostName, $user, $pass, $quizDB, $DBTable); Quote Link to comment https://forums.phpfreaks.com/topic/239383-user-defined-function-help/#findComment-1229849 Share on other sites More sharing options...
kwdelre Posted June 15, 2011 Author Share Posted June 15, 2011 I see what you are saying. I originally put this: function CheckCertStatus () { instead of... function CheckCertStatus ($host, $dbusername, $dbpassword, $TxQuizdb, $QuizResultsTable) { thinking that it was necessary because $host, etc was located in another file. So I didn't put it there for the right reason. But based on the code I have in the function, do I need $host, etc in the function declaration? Could you give me a child's explanation of these statements? - $Ready is the only variable that I need to take from the function. - In the function itself there are several variables from an include(). I'm just confused about what will go in: function CheckCertStatus (?) { and when implementing CheckCertStatus (?) Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/239383-user-defined-function-help/#findComment-1229855 Share on other sites More sharing options...
mikesta707 Posted June 15, 2011 Share Posted June 15, 2011 You seem to not quite understand how scope works with functions. You say that the values for your mysql function data is contained in another file. You can include this information if you want. However, when you invoke the function, if you don't give the function the data, it won't have it. I'll give a simpler example to explain what I mean. Say we have the following function. The actual definition is unimportant, but pay attention to the argument list function foo($arg1, $arg2){ $arg3 = $arg1 + $arg2;//do something stupid } In the scope of our function foo, it only has access to $arg1 and $arg2 and they are set to whatever we pass into it. To illustrate this, consider the following //first, lets define 2 variables $arg1 = 5; $arg2 = 10; //note, these 2 variables our outside the function, so foo doesn't even know they exist //lets change our function a but. lets return $arg3 function foo($arg1, $arg2){ return $arg1 + $arg2;//do something stupid } echo foo(1,2);//this will echo 3 echo foo($arg1, $arg2);//this echos 15 //note, foo only has access to the $arg1 and $arg2 from the rest of our script if we pass it in echo foo();//this will give us an error. remember, foo doesn't even know the 2 variables we defined first even exist unless we pass it now lets see how this relates to your example. You say you try invoking your function like this CheckCertStatus () but remember, if you don't pass anything to the function, not only does its arguments not have values, but PHP won't even let you continue execution and will throw an error! Now you say you have defined these variables in another function, lets say for example, config.php. Like our example above, we must remember that a function has its own scope, and even though the rest of the script has access to your variables from config.php the function doesn't. Thus we have to pass it in. Check Nightslyr's post for details about this, as he talks about this pretty thoroughly. You ask if you should include these variables in your function declaration (specifically the argument list), and the answer is yes. Well, Yes for the most part. If you wanted to you could accomplish what you want using the global keyword or some other nonsense, but this is more or less the best, most standard way. The reason is, as I have stated, if you don't pass the information into the function, the values and variables you need won't be available Quote Link to comment https://forums.phpfreaks.com/topic/239383-user-defined-function-help/#findComment-1229859 Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 Functions utilize their own scope. Any variables defined inside a function only exist within that function, unless explicitly returned. Likewise any variable created outside a function only exists outside the function unless explicitly passed-in. Here is what I would do. Create your MySQL connection somewhere else. Maybe in the file that has the credentials, or just at the top of your script. Then you don't need to pass anything to your function, as the MySQL connection has already been established. Quote Link to comment https://forums.phpfreaks.com/topic/239383-user-defined-function-help/#findComment-1229866 Share on other sites More sharing options...
kwdelre Posted June 15, 2011 Author Share Posted June 15, 2011 Thank you very much! You guys really helped explain it for me. Guess I just needed to hear it a couple different ways. Thanks again! You seem to not quite understand how scope works with functions. You say that the values for your mysql function data is contained in another file. You can include this information if you want. However, when you invoke the function, if you don't give the function the data, it won't have it. I'll give a simpler example to explain what I mean. Say we have the following function. The actual definition is unimportant, but pay attention to the argument list function foo($arg1, $arg2){ $arg3 = $arg1 + $arg2;//do something stupid } In the scope of our function foo, it only has access to $arg1 and $arg2 and they are set to whatever we pass into it. To illustrate this, consider the following //first, lets define 2 variables $arg1 = 5; $arg2 = 10; //note, these 2 variables our outside the function, so foo doesn't even know they exist //lets change our function a but. lets return $arg3 function foo($arg1, $arg2){ return $arg1 + $arg2;//do something stupid } echo foo(1,2);//this will echo 3 echo foo($arg1, $arg2);//this echos 15 //note, foo only has access to the $arg1 and $arg2 from the rest of our script if we pass it in echo foo();//this will give us an error. remember, foo doesn't even know the 2 variables we defined first even exist unless we pass it now lets see how this relates to your example. You say you try invoking your function like this CheckCertStatus () but remember, if you don't pass anything to the function, not only does its arguments not have values, but PHP won't even let you continue execution and will throw an error! Now you say you have defined these variables in another function, lets say for example, config.php. Like our example above, we must remember that a function has its own scope, and even though the rest of the script has access to your variables from config.php the function doesn't. Thus we have to pass it in. Check Nightslyr's post for details about this, as he talks about this pretty thoroughly. You ask if you should include these variables in your function declaration (specifically the argument list), and the answer is yes. Well, Yes for the most part. If you wanted to you could accomplish what you want using the global keyword or some other nonsense, but this is more or less the best, most standard way. The reason is, as I have stated, if you don't pass the information into the function, the values and variables you need won't be available Quote Link to comment https://forums.phpfreaks.com/topic/239383-user-defined-function-help/#findComment-1229878 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.