SitePoint Sponsor

User Tag List

Page 4 of 4 FirstFirst 1234
Results 76 to 79 of 79
  1. #76
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Ok, at this point it seems prudent to discuss the issue of "scope". Scope is a major but necessary gotcha in all programming languages, and many of the differences between languages are how they handle scope.

    Scope refers to the variables visible to the currently running code. We say a variable is "in scope" if it can be accessed. If it can't, it's not in scope. A variable can share a name with a variable that's not in scope.

    PHP Code:

    $a 
    4;

    function 
    foo() {
      
    $a 3;
      return 
    $a;
    }

    echo 
    $a// 4
    echo foo(); // 3
    echo $a// 4. 
    Scope is a nuisance in small projects and the temptation is to use the global keyword to bring a variable into the scope of the function. The problem is this makes the function dependent on that variable and it becomes difficult to impossible to test the function in a large project. The solution is simple - don't use global.

    If a function needs to "see" a value then it should receive it as an argument.

    PHP Code:
    function increment$a ) {
      
    $a++;
      return 
    $a;

    But what if a function does need to work with a lot of values? Passing more than two arguments rapidly becomes cumbersome as you have to remember the order you are passing the values as they will have an impact on the output.

    The solution to this problem is the next step of code organization -- Classes and object oriented programming. Once you get used to functions they are the next step. This is the next step up from basic functions - but what classes do is bind functions together with data (and much more, but trying to keep things basic here). When a function is part of a class it is called a "method". When a variable is part of a class it is called a "member" Methods can see the members of the class using the reserved $this value.

    I'll stop there because that's the next step beyond functions and I don't want to muddle the issue too much. The point is to show where the road ahead is going.

  2. #77
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    Ok, at this point it seems prudent to discuss the issue of "scope". Scope is a major but necessary gotcha in all programming languages, and many of the differences between languages are how they handle scope.

    Scope refers to the variables visible to the currently running code. We say a variable is "in scope" if it can be accessed. If it can't, it's not in scope. A variable can share a name with a variable that's not in scope.

    PHP Code:

    $a 
    4;

    function 
    foo() {
      
    $a 3;
      return 
    $a;
    }

    echo 
    $a// 4
    echo foo(); // 3
    echo $a// 4. 
    Scope is a nuisance in small projects and the temptation is to use the global keyword to bring a variable into the scope of the function. The problem is this makes the function dependent on that variable and it becomes difficult to impossible to test the function in a large project. The solution is simple - don't use global.

    If a function needs to "see" a value then it should receive it as an argument.

    PHP Code:
    function increment$a ) {
      
    $a++;
      return 
    $a;

    But what if a function does need to work with a lot of values? Passing more than two arguments rapidly becomes cumbersome as you have to remember the order you are passing the values as they will have an impact on the output.

    The solution to this problem is the next step of code organization -- Classes and object oriented programming. Once you get used to functions they are the next step. This is the next step up from basic functions - but what classes do is bind functions together with data (and much more, but trying to keep things basic here). When a function is part of a class it is called a "method". When a variable is part of a class it is called a "member" Methods can see the members of the class using the reserved $this value.

    I'll stop there because that's the next step beyond functions and I don't want to muddle the issue too much. The point is to show where the road ahead is going.
    Thanks Michael

    Ive been writing functions for the last couple of days and like you said it seems classes and oop seems the next step.

    In my functions I have been returning the function itself, one if it's arguments , a variable inside the function or in some functions no return at all.

    In most cases, I haven't used true or false much yet (meaning I haven't written "return true", "return false" in the function). I understand why to use them and the different type of job they do but at the moment I just haven't seemed to need them.

    Hopefully the more I read and see other peoples functions the more I can incorporate them.

  3. #78
    SitePoint Member
    Join Date
    May 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ethan, how are you progressing with the php programming?

  4. #79
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by netrox View Post
    ethan, how are you progressing with the php programming?
    Hi netrox thanks for asking.

    It's going quite well, oop seems to have made things a bit clearer and functions seem to make alot more sense thank god.

    Once Ive got it sorted Ill put up some functions for other newbies to learn if they come across this thread.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •