Jump to content

global variables


spence911

Recommended Posts

This is a snippet from a simple script that multiplies integers entered into 2 fields on a form (fnumber and snumber). I'm getting an error message that says "Notice: Undefined variable: num". Is this not the proper way to declare a global variable?

<?php

$num;

function multipli(){
global $num;
$num3 = $_POST['fnumber']*$_POST['snumber'];
return $num;
}

echo $num;

?>
Link to comment
https://forums.phpfreaks.com/topic/283727-global-variables/
Share on other sites

no. Putting $num by itself at the top doesn't define it. What exactly is the echo supposed to output? You declare it but you didn't assign anything to it so not only does $num not have a value, but php doesn't even know what type of variable it's supposed to be. Which isn't the end of the world in this case, since php does loose type casting/comparison. Which is why it's a "notice" and not something more serious like a "fatal" level error. IOW it's akin to the asshole grammar nazi wagging his finger at the improper use of "their vs. they're vs. there" - people usually know wtf you meant, but you're technically wrong and there's always someone with nothin' better to do than point it out.

 

If you want to declare and define it, you need give it a value. Since you're using numbers, you should do $num = 0;

Link to comment
https://forums.phpfreaks.com/topic/283727-global-variables/#findComment-1457551
Share on other sites

no. Putting $num by itself at the top doesn't define it. What exactly is the echo supposed to output? You declare it but you didn't assign anything to it so not only does $num not have a value, but php doesn't even know what type of variable it's supposed to be. Which isn't the end of the world in this case, since php does loose type casting/comparison. Which is why it's a "notice" and not something more serious like a "fatal" level error. IOW it's akin to the asshole grammar nazi wagging his finger at the improper use of "their vs. they're vs. there" - people usually know wtf you meant, but you're technically wrong and there's always someone with nothin' better to do than point it out.

 

If you want to declare and define it, you need give it a value. Since you're using numbers, you should do $num = 0;

 

Ok gotcha. I removed the line $num; and I'm still getting the error. I am trying to find the value of $_POST['fnumber'] multiplied by $_POST['snumber']. As far as I know declaring a global variable from within a function will make the variable available outside this function. So why is $num not recognized?

Link to comment
https://forums.phpfreaks.com/topic/283727-global-variables/#findComment-1457562
Share on other sites

Please ignore the code in my first post. There was a mis-type. Code should read as follows:

<?php

function multipli(){
global $num;
$num = $_POST['fnumber']*$_POST['snumber'];
return $num;
}

echo $num;

?>

The line I corrected is this one. It should be:

 

$num = $_POST['fnumber']*$_POST['snumber'];

 

NOT

 

$num3 = $_POST['fnumber']*$_POST['snumber'];

Link to comment
https://forums.phpfreaks.com/topic/283727-global-variables/#findComment-1457563
Share on other sites

Perhaps I'm missing something, but your code doesn't really do anything besides echo an undefined variable. $num is defined as a global variable in the multipli() function. But the function is never called...so the code isn't executed. Even if the function was called, the POST variables being multiplied are assigned to $num3 which isn't used again.

 

EDIT: That last part doesn't apply anymore. :)

Link to comment
https://forums.phpfreaks.com/topic/283727-global-variables/#findComment-1457564
Share on other sites

Instead of using global, have you considered doing something like

<?php
function multipli() {
     return $_POST['fnumber']*$_POST['snumber'];
}
 
$num = multipli();
echo $num;
?>
 
...or
<?php
function multipli() {
     return $_POST['fnumber']*$_POST['snumber'];
}
 
echo multipli();
?>

 

Link to comment
https://forums.phpfreaks.com/topic/283727-global-variables/#findComment-1457565
Share on other sites

Perhaps I'm missing something, but your code doesn't really do anything besides echo an undefined variable. $num is defined as a global variable in the multipli() function. But the function is never called...so the code isn't executed. Even if the function was called, the POST variables being multiplied are assigned to $num3 which isn't used again.

 

EDIT: That last part doesn't apply anymore. :)

 

Thanks CyberRobot. I completely overlooked the fact that I should run the function in order to get the value of $num. I added this after closing the function:

 

multipli();
echo $num;
 
and now it echoes the value of $num.
Link to comment
https://forums.phpfreaks.com/topic/283727-global-variables/#findComment-1457567
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.