Jump to content

global variables


spence911
Go to solution Solved by cyberRobot,

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
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
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
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'];

Edited by spence911
Link to comment
Share on other sites

  • Solution

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. :)

Edited by cyberRobot
Link to comment
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.
Edited by spence911
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.