Iank1968 Posted February 10, 2009 Share Posted February 10, 2009 I understand the using local variables is faster then Global. Here are 2 of the global function I created. Would anyone be able to tell me how to make it into a local variable, if thats the best thing to do. function Query ($q) { global $conn; $result = mysql_query ($q, $conn); if (!$result) { } and the second is function LogStealAttempt ($user_from, $user_to, $what, $amt) { global $logsendneg_table; Query ("INSERT INTO $logsendneg_table SET uid_from=$user_from, uid_to=$user_to, what='$what', amt=$amt"); thanks Quote Link to comment Share on other sites More sharing options...
gevans Posted February 10, 2009 Share Posted February 10, 2009 Instead of declaring you're variable as global, pass it to the function, so your first function would be; function Query ($q, $conn) { $result = mysql_query ($q, $conn); if (!$result) { //code bla bla } } $result = Query($the_query, $the_connection); Quote Link to comment Share on other sites More sharing options...
Iank1968 Posted February 10, 2009 Author Share Posted February 10, 2009 I created quite a few global functions, so I just move the global function inside the function and just get rid of the global? Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 10, 2009 Share Posted February 10, 2009 You have to understand that globals can actually have a purpose. For example, in your case you have a $conn that contains the handle to your mysql db connection. You can certainly do what gevans suggested, but if you do, then you will have to pass the handle variable explicitly to every function. Handles are special variables in that they can't be serialized or copied without issue, so you need to be careful that if you do pass them into functions, that you declare the variable to use pass by reference. So in Gevans example. you probably want: function Query ($q, &$conn) { $result = mysql_query ($q, $conn); if (!$result) { Quote Link to comment Share on other sites More sharing options...
gevans Posted February 10, 2009 Share Posted February 10, 2009 You have to understand that globals can actually have a purpose. For example, in your case you have a $conn that contains the handle to your mysql db connection. You can certainly do what gevans suggested, but if you do, then you will have to pass the handle variable explicitly to every function. Handles are special variables in that they can't be serialized or copied without issue, so you need to be careful that if you do pass them into functions, that you declare the variable to use pass by reference. I wasn't aware of that, that's good to know. What would be the downsides or 'problems' with passing a handle to a function without setting it as a pass by reference? Quote Link to comment Share on other sites More sharing options...
Iank1968 Posted February 10, 2009 Author Share Posted February 10, 2009 ahhh ok. thanks alot for your help on this one. does global function bog your system and app way down as is claimed? Quote Link to comment Share on other sites More sharing options...
Iank1968 Posted February 10, 2009 Author Share Posted February 10, 2009 Does anyone feel like rewriting about 20 global functions I used? lol Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 11, 2009 Share Posted February 11, 2009 You have to understand that globals can actually have a purpose. For example, in your case you have a $conn that contains the handle to your mysql db connection. You can certainly do what gevans suggested, but if you do, then you will have to pass the handle variable explicitly to every function. Handles are special variables in that they can't be serialized or copied without issue, so you need to be careful that if you do pass them into functions, that you declare the variable to use pass by reference. I wasn't aware of that, that's good to know. What would be the downsides or 'problems' with passing a handle to a function without setting it as a pass by reference? Usually it will be garbage/non functional after it's copied, because it's not the original handle anymore. I did mention serialization, but that is a bad example, because serialized handles will always be destroyed. If you think of a handle as a connection to something, it's easier to think about them that way. While you can use a connection to access something, the connection itself has no value. PHP calls this special variable type a "resource". There's more about them here -> http://us3.php.net/manual/en/language.types.resource.php 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.