• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

[PHP] functions

#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.
 
#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?
 
#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
 
#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

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 ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,015
Latest member
bettyicrewsi