[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.
 

Glaanieboy

OSNN Veteran Addict
Joined
6 Mar 2002
Messages
2,628
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?
 

Geffy

OSNN Veteran Addict
Joined
18 Mar 2002
Messages
7,805
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
 

Glaanieboy

OSNN Veteran Addict
Joined
6 Mar 2002
Messages
2,628
Clear :) Thanks for the explaination. I have found another (not so neat) solution for my problem though.
 

ignipotentis

OSNN Addict
Joined
25 Mar 2004
Messages
127
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

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.
Terrahertz wrote on Electronic Punk's profile.
Yo fellas!
Electronic Punk wrote on Sazar's profile.
Where are you buddy?

Forum statistics

Threads
62,005
Messages
673,461
Members
5,599
Latest member
Larry Chris