Global variables

Trouble using global variable inside a function

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1845 Views - Last Post: 09 June 2010 - 03:48 PM Rate Topic: -----

#1 sheraush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-January 10

Global variables

Posted 08 June 2010 - 04:35 PM

Hi All,

I've been working on calling a global variable inside a function and have tried several ways as I've seen online including:

global $variable;
// AND
$localVariable = $GLOBALS['variable'];


What I'm trying to do is to use this variable as a condition for a for-loop in a function inside my script. I have tested and was able to use both methods above outside the function and was even able to call the global variable inside the function, change it's value and then echo that value from outside said function. My last try was:
$cnt=$GLOBALS['count'];
$count = $cnt;
for($a=0; $a<=$count; $a+=2)


Any help is appreciated.

Thanks,
Newbie to PHP

Is This A Good Question/Topic? 0
  • +

Replies To: Global variables

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,238
  • Joined: 08-June 10

Re: Global variables

Posted 08 June 2010 - 04:37 PM

er, and what is the question now?
Was This Post Helpful? -1
  • +
  • -

#3 KingCuddles  Icon User is offline

  • D.I.C Regular

Reputation: 176
  • View blog
  • Posts: 496
  • Joined: 20-December 08

Re: Global variables

Posted 08 June 2010 - 04:39 PM

Consider the following example.

//create our variable to be used anywhere
global $myMessage;

//define our function
function echoMessage ($message) {
echo "$message";
}

//give $myMessage a value
$myMessage = "hello";

//call the echoMessage function and populate it with $myMessage
echoMessage($myMessage);


Also always do your research first.

Edit - Added a better example, and reference to the PHP manual, also good post JackOfAllTrades

This post has been edited by KingCuddles: 08 June 2010 - 04:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Global variables

Posted 08 June 2010 - 04:52 PM

I explained this a little in this post. Maybe what I said there will help?
Was This Post Helpful? 0
  • +
  • -

#5 sheraush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-January 10

Re: Global variables

Posted 09 June 2010 - 07:23 AM

@KingCuddles Thanks for replying, always good to get ANY reply on questions posted. But for the record, I did do my research first as stated in my post I've tried several ways however, the variable, although declared global inside the function was unable to be used as a condition in my for-loop.

@JackOfAllTrades Thanks for your reply, I've tried your example also but doesn't help my situation either.

I fully understand the logic of making the variable global inside the function, however, I'm having trouble using that variable in a loop.

Thanks anyway.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Global variables

Posted 09 June 2010 - 07:28 AM

Can you show us the EXACT CODE you're using? A variable is a variable...how you use it is immaterial. For example:
<?php

function theFunction()
{
    global $theGlobal;
    for ($i = 0; $i < $theGlobal; ++$i)
        echo "$i\n";
}

$theGlobal = 5;
theFunction();
?>


yields
php -f global.php
0
1
2
3
4


Was This Post Helpful? 0
  • +
  • -

#7 sheraush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-January 10

Re: Global variables

Posted 09 June 2010 - 07:59 AM

Here's how my code is set up:
<?php

// MODULE PARAMETERS
$count = (int) $params->get('maxCount','18');
$tabText = $params->get('tabText');
$contentColor =  $params->get('contentColor');

echo "count = $count outside the function."; 
// this ouputs: count = 18 outside the function.

// FUNCTION TO SHOW CATEGORIES UNDER A SECTION
function showCategories($parent)
{
global $count, $tabText, $contentColor;
echo "count = $count inside the function."; 
//this ouputs: count = inside the function.

// some code for DB connection
	
	
$b = 1;
echo "<table width=100%>";
for($a=0; $a<=$count; $a+=2)
{	
	echo "<tr>";
	echo "<td width=50%> <a href='?category=$itemArray[$a]'> $itemArray[$a] </a> </td>";
	$b = $a+1;
	echo "<td width=50%> <a href='?category=$itemArray[$b]'> $itemArray[$b] </a></td>";
	echo "</tr>";
}
echo "</table>";
	
mysql_close($strConnect);
}// END FUNCTION SHOWCATEGORIES()



As you can see here, the only reason I need this global variable is for that loop inside the function, because of the nature of the code, I can't use the statement to assign the value to this variable inside the function as it has to be done in the first lines of the script.

I'm really not sure how to use this variable inside this function because the only logical way I've seen it used in the PHP Manual and in this post doesn't seem to work in my case.

Any ideas, or help is appreciated.

Thanks!

This post has been edited by sheraush: 09 June 2010 - 08:03 AM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Global variables

Posted 09 June 2010 - 08:04 AM

Where are you calling showCategories? Is this an included script?
Was This Post Helpful? 0
  • +
  • -

#9 sheraush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-January 10

Re: Global variables

Posted 09 June 2010 - 08:06 AM

That's called in an html section of the same file. No includes.
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Global variables

Posted 09 June 2010 - 08:08 AM

Can I just ask why you aren't passing the values as parameters? That's the preferred method (pardon the pun) here.
Was This Post Helpful? 0
  • +
  • -

#11 sheraush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-January 10

Re: Global variables

Posted 09 June 2010 - 08:18 AM

Good question, will try that right now and see if I can see the globals from inside the html code. This is weird.

Thanks for your help. I know it must be annoying when it seems newbs don't do their own research but I assure you, I've spent countless hours yesterday reading about variables and trying to figure out what I'm not doing in my code. I really appreciate your help on this.

Ok, no luck, here's what the code looks like now:

<html>
<div id="fragment-1" class="content">
       <?php
	$parent=1;
	showCategories($parent,$count);
	?>
...
</html>

<?php

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

// MODULE PARAMETERS
global $count, $tabText, $contentColor;
$count = (int) $params->get('maxCount','18');
$tabText = $params->get('tabText');
$contentColor =  $params->get('contentColor');


echo "count = $count outside the function.";
// outputs: count = 18 outside the function.

// FUNCTION TO SHOW CATEGORIES UNDER A SECTION
function showCategories($parent,$count)
{
echo "count = $count inside the function.";
// outputs: count = inside the function.
  
// DB STUFF

$b = 1;
echo "<table width=100%>";
for($a=0; $a<=$count; $a+=2)
{	
	echo "<tr>";
	echo "<td width=50%> <a href='?category=$itemArray[$a]'> $itemArray[$a] </a> </td>";
	$b = $a+1;
	echo "<td width=50%> <a href='?category=$itemArray[$b]'> $itemArray[$b] </a></td>";
	echo "</tr>";
}
echo "</table>";
mysql_close($strConnect);
}// END FUNCTION SHOWCATEGORIES()



This post has been edited by sheraush: 09 June 2010 - 08:20 AM

Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Global variables

Posted 09 June 2010 - 08:46 AM

OK, so your HTML which is calling your function precedes your PHP block in which you've defined the global. This is the crux of the matter, as PHP is processing from the top of the file down. You need those variables to have values at the time the function is called, so that must occur before that point in the file.
Was This Post Helpful? 1
  • +
  • -

#13 sheraush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-January 10

Re: Global variables

Posted 09 June 2010 - 09:09 AM

That doesn't work, I've commented out everything else inside the function and it seems it doesn't recognize the "global" at all. I give up, starting over, with very simple example testing first to see where exactly the problem is.
Was This Post Helpful? 0
  • +
  • -

#14 sheraush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-January 10

Re: Global variables

Posted 09 June 2010 - 01:10 PM

In case anybody else encounters this problem...

The order it needed to be done is as follows:
1.) declare variables as global
2.) assign values to variables
3.) inside function, declare variable as global
4.) use as needed
global $x, $y;
$x = 1;
$y = 2;

function sum()
{
global $x, $y;
$result = $x+$y;
echo $result;
}

sum();



At least that's in respect to coding Joomla! Module parameters. I believe JackOfAllTrades' method works as he said they would otherwise.

This post has been edited by sheraush: 09 June 2010 - 01:14 PM

Was This Post Helpful? 0
  • +
  • -

#15 mahcuz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 213
  • Joined: 03-June 10

Re: Global variables

Posted 09 June 2010 - 01:18 PM

JackOfAllTrades explained the problem.

You give the $x variable its value *after* you call the function. You need to define its value prior to the function call.

Edit: I see you figure it out. Also, that first 'global' is redundant. Also, stop using globals.

This post has been edited by mahcuz: 09 June 2010 - 01:20 PM

Was This Post Helpful? -1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2