#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    126
    Rep Power
    11

    PHP Functions are like subroutines?


    I am having a hard time for some reason understanding php classes and functions. I have read the php manual and many other examples to no avail. Am I to understand that a function is like a subroutine? Point is, every example I come across shows functions just echo something. Like:

    PHP Code:
    function foo() {
    echo 
    "bar";
    }
    foo(); 
    I know the result is bar.

    Well, I want to call on some function to do more than echo some text. Can anyone point me to an indepth tutorial on php functions and classes other than the php.net mans and docs?

    Thanks in advance.
  2. #2
  3. Trapped on the forums...help
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2003
    Location
    /Users/edman007
    Posts
    4,617
    Rep Power
    905
    try zend.com they have better tutorials then php.net
  4. #3
  5. fan boy..................
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Los Angeles, California
    Posts
    299
    Rep Power
    11
    PHP Code:
    function DoStuff($a,$b) {
         
    $c=$a+$b;
         echo 
    $c;
    }
    $one=1;
    $two=2;
    $three=3;
    $four=4;
    DoStuff($one,$four);
    //outputs 5
    DoStuff($four,$two);
    //outputs 6
    DoStuff($three,$four);
    //outputs 7 
    That example should give you the idea
  6. #4
  7. No Profile Picture
    aHVoPw==
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2001
    Posts
    1,058
    Rep Power
    15
    To edit ibejohn's code a bit, just to show that a function doesn't necessarily have to "print" data. It can "return" data, which can be used for other things in the code.
    PHP Code:
    function DoStuff($a,$b) { 
         
    $c=$a+$b
         return 
    $c

    $one=1
    $two=2
    $three=3
    $four=4
    $var1DoStuff($one,$four);
    echo 
    "1 + 4 is " $var1;
    //outputs "1 + 4 is 5" 
    Basically a function is a like a little environment of it's own. It has it's own variables and such. So from the example above, if I try to print $c outside of the function, it would not have a value because it is only set for inside the function (of course you can create $c outside the function also, and they will both be totally unrelated).
    Visit my new IIS Tutorial Series!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    126
    Rep Power
    11
    Can conditionals and loops be set inside functions? Or are functions just for add register items and such?

    An example such as:

    PHP Code:
    function privies() {

    $priv mysql_query("SELECT * FROM $users WHERE nick = '".addslashes($nick)."' AND password = '".addslashes($passw)."'");
    while(
    $row mysql_fetch_row($priv)) {
        
    $nick $row[1];    //  nick
        
    $email $row[3];   //  email
        
    $post $row[5];    //  post_news
        
    $edit $row[6];    //  edit_own_news
        
    $del $row[7];     //  delete_own_news
        
    $addr $row[8];    //  add_replies
        
    $editr $row[9];   //  edit_own_replies
        
    $delr $row[10];   //  delete_own_replies
        
    $ban $row[13];    //  banned_ip
        
    $editn $row[14];  //  edit_news
        
    $deln $row[15];   //  delete_news
        
    $editr $row[16];  //  edit_replies
        
    $delr $row[17];   //  delete_replies
        
    $actn $row[18];   //  activiate_news
        
    $dactn $row[19];  //  deactivate_news
        
    $clsn $row[20];   //  close_news
        
    $addu $row[21];   //  add_users
        
    $editu $row[22];  //  edit_users
        
    $delu $row[23];   //  delete_users
        
    $banu $row[24];   //  ban_users
        
    $editc $row[25];  //  edit_config
        
    $admin $row[26];  //  is_admin
        
    $mod $row[27];    //  is_moderator
        
    $user $row[28];   //  is_user
                                
    };
            if (
    $ban == 1) {
                echo 
    "You are banned!";
                exit; }
            elseif ((
    $admin == 1) || ($mod == 1) || ($user == 1)) { 
                
    $login "Welcome back <b>$nick</b>!"; }
            else { 
    $login "<a href=$web/login.php>Login</a> or <a href=$web/register.php>Register</a>"; }
    }

    privies(); 
    I know the above does not work. But, it is an example of something I would like to use (try?) as a function to call upon when needed. Another question is, when functions set variables are they set outside the function as well after it is called? Meaning, I can use the $vars set by a function wherever and whenever in some other conditional if needed? (Like the above example.)

    Sorry to seem so lost here. Your examples are exactly what I find when I google and I understand them completely. I do appriciate the help! I am just wondering if I can do more with a function.
  10. #6
  11. No Profile Picture
    Rehistradong manggagamit ...
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    Cavite, Philippines
    Posts
    116
    Rep Power
    11
    Yes, you can do loops within a function.

    About variables, if you set a value to a variable within a function, the variable's value will be lost when the function ends, like so....

    PHP Code:
    function me () {
      
    $sum 3;

      print 
    $sum;
    }

    me();
    print 
    $sum;

    // Output is
    //
    // 4
    // 
    The second $sum has no value, so it echoes nothing.

    But if your setting a value to a global variable (session variables?) it will also take effect when you exit the function.

    PHP Code:
    function me () {
      print 
    $_SESSION['sum'];
      
    $_SESSION['sum'] = 3;

      print 
    $_SESSION['sum'];
    }

    $_SESSION['sum'] = 1;
    me();
    print 
    $_SESSION['sum'];

    // Output is
    //
    // 1
    // 4
    // 4
    // 
    JP
  12. #7
  13. Unknown column 'intelligence'.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    54
    Rep Power
    11
    No, like has been said, variables within functions are set only within the function. Also, if variables have been set within the script, the function won't recognize this: they have to be passed to it within its argument.

    Your above example would have to have privies($nick), otherwise it's not getting the $nick variable from anywhere.

    Functions can return arrays: therefore more than one bit of information can be returned be a particular function.
  14. #8
  15. Trapped on the forums...help
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2003
    Location
    /Users/edman007
    Posts
    4,617
    Rep Power
    905
    Originally Posted by jaypeesol
    ........But if your setting a value to a global variable (session variables?) it will also take effect when you exit the function.
    ........
    using sessions for that is a wast, if you are already using sessions and need that particular variable then it would be ok, but i wouldn't recommend it, instead you can make a variable global so it acts lkinda ike superglobal ($_SESSION, $_POST, $_GET, $_COOKIES, $_FILES and so on) all you have to do is tell php what variables you want to be global, like this

    PHP Code:
    function me(){
    global 
    $sum;//now $sum will be accessible from inside the function as well as outsite it
    $sum++;
    echo 
    $sum;
    }
    $sum =0;
    echo 
    $sum;
    me();
    me();
    echo 
    $sum;
    me();
    $sum 175;
    me();
    $sum += 5;
    me();
    echo 
    $sum;

    the above code will echo this:
    //0
    //1
    //2
    //2
    //3
    //176
    //182
    //182 

    now here is my definition of what a function is, it is something that lets you use many lines of code over and over, making the end product take less code as well as easier on the eyes to read the code, since variables inside a function aren't accessible from outside it it allows you to right code with worrying about what the names of the variables you are using so you don't overwrite a needed variable making it easier to code

    then there are times when you have to make a function to get a job done, for example sometimes you want to create something that has to perform a loop of code inside a loop of code inside a loop of code, each loop is the same structure but the amount of sub loops is defined by a counter, there are only 2 ways to get it done, either make hundreds or even thousands of if(){}elseif(){} statements for each possible value of the counter, or create one function with just a few lines of code that calls itself from inside the loop incrementing the counter as needed for an almost infinite amount of sub loops

    then you need functions for the preg_replace_callback() and functions like that instead of using preg_match_all, getting the match, then trying edit the match and put it back in you can make a function that preg_replace_callback() will call, that function can do all the work you need to edit the matches and return the string to replace the match with your new edited string that can now contain something other then just moving the matches around and adding static data
  16. #9
  17. No Profile Picture
    Rehistradong manggagamit ...
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    Cavite, Philippines
    Posts
    116
    Rep Power
    11
    Great job, edman007!
    JP
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    126
    Rep Power
    11
    Yes, great job Az` AND edman007. That is exactly what I needed to hear.

    I thank you to everyone for responding. I now have an understanding of how functions are used and implemented!

IMN logo majestic logo threadwatch logo seochat tools logo