Can you explain me a few things of this custom function?

Discussion in 'PHP' started by x0x, Aug 23, 2008.

  1. #1
    function alertadminaboutspamm($msg=""){
    global $tab, $time, $mainadmin, $senderip;
    
    $getstuff = **not important**
    
    mysql_query("**not important**);
    
    mysql_query(**not important**);
    
    return 100;
    }
    
    
    PHP:
    The "global" I understand I think, you have to make the variables global to work inside the function, which is pretty useless. They should all be accessible.

    Anyway, what I don't understand what is the point of "return" there? And 100 behind it?
    What does it do? I read about it in php.net but I still don't get it. It un-globalizes or ends the function or what?

    Also, in another function I notied a php variable after "return.
    return $infos;
    What did that mean? I'm eager to learn php.
     
    x0x, Aug 23, 2008 IP
  2. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    return is so that it can send back a value. For instance, when you use a function, eg stripslashes(), it returns the stripslashed data you put though it. For instance, using that function, if you ran $var = alertadminaboutspamm(), $var would have the value 100.

    As for global, it isn't useless, you use it to access variables from outside the function without passing them to the function inside the brackets.

    return $infos would make $var = function_name_here_with that_return_$infos_statement_here() be equal to the value of $infos that the function would decide upon.

    I probably haven't done a very good job of explaining, I'll try and find some good online tutorials to explain it better.
     
    Pos1tron, Aug 23, 2008 IP
  3. ForumJoiner

    ForumJoiner Active Member

    Messages:
    762
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    83
    #3
    A function is takes some parameters (that is, input variables), use a recipe and gives you a result. That result can be a constant (like 100), a variable (like $myvar) or some other things. The result is the output of the function.

    That 100 can be used by other function. For instance, if 100 is an error message, the blunt way to display is:
    Error 100.

    The nice way is "You finally encountered the error 100, which means that you forget to ...".
    function NiceMessage cand take the 100 value and generate the nice text.

    Why don't generate the nice text directly? Good question :)
    Suppose you want to use your program in several languages.
    Detect language ... en
    Use NiceMessageEN, which will show the error message in English.
    If the detected language is Chinese, then the error message will be displayed in Chinese.
     
    ForumJoiner, Aug 23, 2008 IP