Jump to content

[SOLVED] Global Variables In Functions


jjacquay712

Recommended Posts

I am trying to make the variable $times accessible outside of the function. I am using the global keyword to declare the variable. Here is my code:

 

<?php
$string = $_GET['text'];
$string =  eregi_replace(" +", " ", $string);
$sort = explode(" ", $string);

global $times; //Look Right Here**************************************

function sortme($array) {
$num_times = (count($array)) - 1;
$again = true;
while ( $again ) {
	$again = false;
	for ( $i = 0; $i < $num_times; $i++ ) {
		if ( $array[$i] < $array[$i + 1] ) {
			$mem = $array[$i + 1];
			$array[$i + 1] = $array[$i];
			$array[$i] = $mem;
			$again = true;
			$times++; //Look Right Here**************************************
		}
	}
}
return $array;
}


$array = sortme($sort);
foreach ($array as $dim => $val) {
echo $dim . ' => ' . $val . '<br />';	
}

echo '<br /><br />Number Of Flips: ' . $times; //Look Right Here**************************************

?>

 

 

Am I doing it wrong?

Link to comment
Share on other sites

It's not a matter of necessity, but of good practice.  Sure, you can code around it, but later on down the road, your code will be harder to follow and debug and expand upon, because you're just not sure what can be changed where.  That's the point of staying within scope. 

 

If you pass the value of something to a function, the function creates its own local variable.  What it does inside that function will not change the variable on the outside.  That way, you don't have to worry about your variable being overwritten if you need that original value later on.  If you want the value returned to the outside world, you can return something from a function. 

 

If you were to try and use global variables in the same situation, you would have to make a temporary copy of the variable, then mess around with it, then assign the original value back to the global variable.  When you pass something through an argument, you don't have to do all that; php does that step for you.  Like Vegas, you know that whatever happens in the function, stays in the function. 

 

Same thing for classes.  Classes have even more layers of scope.  You can have variables accessible to code outside of the class (public). You can also make them only accessible within the class that made them (private), and accessible within its own class and any class that extends that class (protected). 

 

The whole point in functions, and moreso, object oriented programming, is that you have a piece of code that is self-contained so you don't ever have to touch the insides of it, ever again.  In order to adhere to that philosophy, you must contain the scope of the variables. 

 

You don't want people being able to mess around with internal variables, from the outside.  Doing so makes the function unstable.  The only way you can have a stable, secure function or method or class is if you know for absolute certain that no matter what, when you call it, it's going to do the same thing, every single time.  Allowing code to disregard scope breaks that security. 

 

I'm sorry you read in some book that it's okay to do that, but it's not.  It's not good programming practice. 

Link to comment
Share on other sites

Ok, that all makes perfect sense. But for example, in this case you have a function that needs to return two sets of data (or should if we keep in scope). One being an array and the other a varialbe. What's the best way to do this within the confinds of a function...?

Link to comment
Share on other sites

In you have to return more than what can be done with a variable or an array, you should either be using a class or the functionality of the code in the function is not well defined and should probably be part of the main code and not a function.

 

Functions should be building blocks that perform useful and discrete operations that your application needs.

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.