[PHP] functions

Glaanieboy

OSNN Veteran Addict
Joined
6 Mar 2002
Messages
2,628
I am a total n00b when it comes to PHP functions, therefore I ask you to help me. I have come as far as this:
Code:
function pizza_data($pizzaid){
  $search_pizza = mysql_query("SELECT * FROM pizza WHERE pizzaid=$pizzaid");
  $fetch_pizza = mysql_fetch_array($search_pizza);
  $pizza_data = $fetch_pizza['pizza'];
  return $pizza_data;
}

...
(somewhere in here, $pizzaid gets a valid value)
...

pizza_data($pizzaid);
print $pizza_data;

But that doesn't work, I get no value for $pizza_data. What magical trick should I apply to make this work? The queries are OK and when I use the function code as seperate code, it also works.
 
Did a bit more testing, and found some examples on http://perl.about.com/library/weekly/aa112102a.htm, like this one:
Code:
$x = 3;
print square($x);

function square($num)
{
  return $num * $num;
}
And this works flawlessly.

But when I alter the code to this (what I want), I get null results:
Code:
function square($num)
{
  $num = $num * $num;
  return $num;
}
$x = 3;
square($x);
print $num;

According to the website, it should work, but it doesn't, or am I missing something here?
 
Code:
function pizza_data($pizzaid){
  $search_pizza = mysql_query("SELECT * FROM pizza WHERE pizzaid=$pizzaid");
  $fetch_pizza = mysql_fetch_array($search_pizza);
  $pizza_data = $fetch_pizza['pizza'];
  return $pizza_data;
}

...
(somewhere in here, $pizzaid gets a valid value)
...

pizza_data($pizzaid);
print $pizza_data;

ok cept for
Code:
pizza_data($pizzaid);
print $pizza_data;

try

Code:
$pizza_data = pizza_data($pizzaid);
print $pizza_data;

the $pizza_data in the pizza_data() function only has local scope to that function so when you return it you need to effectively have a place for that returned data to go, like a variable on the other side of the = from the function

btw in the second set of examples you give its returning the output to a print function

so it sends the functions result directly to be printed, you on the other hand are just calling the function and returning its result to nothingness :p
 
Clear :) Thanks for the explaination. I have found another (not so neat) solution for my problem though.
 
php is not a functional language. you cannot just square x. If you want to code in the style you were using before Geffy corrected you, check out ocaml, or lisp.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,620
Latest member
alexexists11
Back