spence911 Posted November 8, 2013 Share Posted November 8, 2013 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; ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted November 8, 2013 Share Posted November 8, 2013 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; Quote Link to comment Share on other sites More sharing options...
spence911 Posted November 8, 2013 Author Share Posted November 8, 2013 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? Quote Link to comment Share on other sites More sharing options...
spence911 Posted November 8, 2013 Author Share Posted November 8, 2013 (edited) 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 November 8, 2013 by spence911 Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted November 8, 2013 Solution Share Posted November 8, 2013 (edited) 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 November 8, 2013 by cyberRobot Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted November 8, 2013 Share Posted November 8, 2013 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(); ?> Quote Link to comment Share on other sites More sharing options...
spence911 Posted November 8, 2013 Author Share Posted November 8, 2013 (edited) 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 November 8, 2013 by spence911 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.