I have been learning a ton of PHP lately in an effort to build a significant amount of custom functionality into a Drupal website. As I am not a developer by trade it has been a steep climb upward. Lately I have been refactoring a lot of my code using functions. Being a noob however, I ran into some issues in that I didn’t understand how PHP scoped the usage of variables.

Case in point, I had written a function to generate a bunch of variables dynamically based on some input into the function, but I wasn’t able to use those variables outside of the function.

So I initially had something along the lines of this extremely simplified example:

<?php
function make_variables($nodeid){
if ($nodeid == 90) {
$testvariable = true;
$usern = bob;
} else {
$testvariable = false;
$usern = joe;
}
}
make_variables(90);
echo $testvariable;
echo "<br>";
echo $usern;
?>

Anyone who codes more regularly than I with PHP will immediately know what result I got. It wasn’t “true” and “bob”… It was error message saying that $testvariable and $usern effectively don’t exist. I do have some familiarity with coding (Lots of Bash, some Python) and immediately figured it might be related to scoping. Essentially, scoping means that those variables ONLY exist within the function. So I can’t call them outside of the function.

Now, if I only needed a single result I could just use a “return” statement. However I need more than one value and a function, by design, can only “return” a single result.

A lot of reading and many hours later I was trying to declare use globals… However some of the wonderful contributors at stack overflow pointed out that defaulting to the use of Global variables isn’t a great habit to pick up and also pointed me towards trying to use an array. Drupal, for whatever reason, also wasn’t behaving as expected when trying to use Globals. Thankfully PHP is also a much used and well documented language and I finally came across exactly the solution I needed. By returning an array and then using the php “list” function one can extract as many values as needed.

My final code looked like this:

<?php
function make_variables($nodeid){
if ($nodeid == 90) {
$testvariable = true;
$usern = bob;
} else {
$testvariable = false;
$usern = joe;
}
return array ($testvariable, $usern);
}
list ($testvariable, $usern) = make_variables(90);
echo $testvariable;
echo "<br>";
echo $usern;
?>

This properly spit out:
true
bob

List (at least in my use case… it may have other uses…) will take a bunch of variable names and assign them a value by iterating through an array. Because my function now returns an array, I can use list to iterate through the returned array and declare all the variables I need outside of the array.

What else is nice is that because we are avoiding the use of “globals” the code should be easier to debug and maintain in the long run.

REFERENCES:
http://php.net/manual/en/language.variables.scope.php
http://php.net/manual/en/function.list.php
http://php.net/manual/en/functions.returning-values.php
http://stackoverflow.com/questions/3415710/php-get-variable-from-function

1 of 1

This post has no comments. Be the first to leave one!

Leave a Reply