[PHP] functions

Discussion in 'Web Design & Coding' started by Glaanieboy, Mar 15, 2004.

  1. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    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. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    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. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    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
     
  4. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    Clear :) Thanks for the explaination. I have found another (not so neat) solution for my problem though.
     
  5. ignipotentis

    ignipotentis OSNN Addict

    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.