Jump to content

How fo I use local variable instead of Global?


Iank1968

Recommended Posts

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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) {

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.