[PHP] functions

Glaanieboy

OSNN Veteran Addict
Joined
Mar 6, 2002
Messages
2,626
#1
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
Mar 6, 2002
Messages
2,626
#2
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
Mar 18, 2002
Messages
7,805
#3
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
Mar 6, 2002
Messages
2,626
#4
Clear :) Thanks for the explaination. I have found another (not so neat) solution for my problem though.
 
Joined
Mar 25, 2004
Messages
127
#5
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 posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Hey ep!

All good with me, applying for microsoft mvp right now, should have done this a while ago.

Notifications don't work, I only found your response by comming back to hunt up some threads, if you want, give me your email address so we can keep in touch easier, mine is perriscalderon at gmail
Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...

Forum statistics

Threads
62,011
Messages
673,481
Members
89,034
Latest member
nasty12