SitePoint Sponsor

User Tag List

Page 3 of 4 FirstFirst 1234 LastLast
Results 51 to 75 of 79
  1. #51
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    function check_rtn ($moves){
     if (
    is_int($moves) && $moves !=''){
      echo 
    "Yes yes it's a number and you caught it";
     }
    }
    $test 7;
    check_rtn($test); 
    Does this piece of code work for you? And if you make test an empty string instead of 7?

  2. #52
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    PHP Code:
    function check_rtn ($moves){
     if (
    is_int($moves) && $moves !=''){
      echo 
    "Yes yes it's a number and you caught it";
     }
    }
    $test 7;
    check_rtn($test); 
    Does this piece of code work for you? And if you make test an empty string instead of 7?
    No that didn't work. I got this error with both

    ( ! ) Fatal error: Cannot redeclare check_rtn() (previously declared in C:\wamp\www\userlogin\writing functions.php:39) in C:\wamp\www\userlogin\writing functions.php on line 55

    But the way you explained in your last post (commenting out echo and just call the function) did the trick with the other function.

  3. #53
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Ethan-27 View Post
    ( ! ) Fatal error: Cannot redeclare check_rtn() (previously declared in C:\wamp\www\userlogin\writing functions.php:39) in C:\wamp\www\userlogin\writing functions.php on line 55
    Because you already have a function with the name check_rtn (your own) in that php file
    You can't declare two functions with the same name in one script. How would the PHP parser know which one you're calling?

  4. #54
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm gonna put my head on the block for this but if you add the '&' symbol to the variable arguement (even though it's a global) would that make a difference..?

  5. #55
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Ethan-27 View Post
    ...would that make a difference..?
    Regarding to what? Why would you want to use a pointer?
    even though it's a global
    What is a global?

  6. #56
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Regarding to what? Why would you want to use a pointer?

    I was going to use one to affect the return of the code like I did here

    PHP Code:
    function basic_prg (&$sum){ 

    $sum $sum *10;

    }

    $number1 15;

    basic_prg($number1);

    echo 
    "$number1  <br/>"
    The most recent example 'Cups' gave me looked similar to this but now I can see they are not really that similar.


    What is a global?
    $moves is a global variable, well I hope it is but I think I might have just noticed something, s**t

  7. #57
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Ethan-27 View Post
    $moves is a global variable, well I hope it is
    Why should it be?

  8. #58
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Regarding to what? Why would you want to use a pointer?
    & means passing by reference, not pointer. They are similar, but different concepts!

  9. #59
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Why should it be?
    I think I may have seen where I'm going wrong. I set $moves as global in one of the functions above this one. I thought that all the other functions using the variable with the same name were using the original global

  10. #60
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Ethan-27 View Post
    I think I may have seen where I'm going wrong. I set $moves as global in one of the functions above this one. I thought that all the other functions using the variable with the same name were using it.
    Exactly
    Using global makes it global in the scope of that 1 function.

  11. #61
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Exactly
    Using global makes it global in the scope of that 1 function.
    Good to know Im getting somewhere but on the other hand all this time I thought the string in the "function check_rtn" was displaying because the function thought that $moves was the global from "function_adding" and that it was returning an integer value of 150, which is the value that "function_adding" created.

    Thanks again guys

  12. #62
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    No it didn't "recognize" it. You sent it:
    Code:
    function check_rtn ($moves) {
      if (is_int($moves) && $moves !='')
        echo "Yes yes it's a number and you caught it";
    }    
    
    echo check_rtn($moves);
    That's what function arguments are for.
    Sure, global variables may seem easier to use (no need to write them out each time you call the function), but they are easier to cause errors as well.

  13. #63
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    No it didn't "recognize" it. You sent it:
    Code:
    function check_rtn ($moves) {
      if (is_int($moves) && $moves !='')
        echo "Yes yes it's a number and you caught it";
    }    
    
    echo check_rtn($moves);
    That's what function arguments are for.
    Sure, global variables may seem easier to use (no need to write them out each time you call the function), but they are easier to cause errors as well.
    Yeh, that variable $moves that I "sent" isn't the global $moves I declared in the previous function....right...

  14. #64
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    No it's the variable $moves that you created in the script (before you called the previous function). If it didn't exist in the script already, there would be nothing to become global inside the function.

  15. #65
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhh man my brain is now completely fried... The only place I declared the global $moves was in a previous function. Ive just deleted the global part of it and the string still displays but im getting another now saying "undefined variable"

    I thought the whole point of a global variable was so you can use it out of the local scope of a function, hence call it somewhere else.

    Someone please shoot me.

  16. #66
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Putting global in front of a variable takes the variable from outside the function scope (ie the global scope) and makes it available inside the function, not the other way around

  17. #67
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    Putting global in front of a variable takes the variable from outside the function scope (ie the global scope) and makes it available inside the function, not the other way around
    Thanks mate, Im getting all mixed up because Im stressed. I'm sure 'define; is confusing me somewhere in here aswell.

    PHP Code:
    function adding(){
        
    global 
    $moves;

    $moves $moves 10;

    }
    $moves 15;
    adding();
    echo 
    "$moves <br/>"
    If I was to remove global, '15' wouldn't be parsed...

  18. #68
    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)
    Quote Originally Posted by Ethan-27 View Post
    Hey again guys, been reading up and today Ive written two functions that are really simple and both work.

    PHP Code:
    function basic_prg (&$sum){ 

    $sum $sum *10;

    }

    $number1 15;

    basic_prg($number1);

    echo 
    "$number1  <br/>"


    function 
    adding(){
        
    global 
    $moves;

    $moves $moves 10;

    }
    $moves 15;
    adding();
    echo 
    "$moves <br/>"
    Ok, stop. This is very bad.

    Functions return values so that they do not affect the state of the outside code. If I call "foo($x);" I expect $x to remain unchanged after the function call. Receiving the argument by reference, changing its value and not issuing a return leads to extremely hard to debug code.

    From outside the function, if I want $x to change then I'll assign the returned value of the function back to $x.

    $x = foo($x);

    Why $x has changed is now clear without having to look up the function definition.

    Including global variables in a function is very bad for the same reason - the function's behavior becomes non-intuitive from the outside code.

    Fundamentals time. A function is an operation definition - no more or less. The simplest of functions are little more than aliases for operations.

    Code php:
     
    function sum($a, $b) {
      return $a + $b;
    }
     
    function isGreater($a, $b) {
      return $a > $b;
    }

    You can have the computer react to the return by putting it in a conditional

    Code php:
    if (isGreater($a, $b)) {
      // do this if function returns true
    } else {
      // do this if function returns false
    }

    Or you can store the return of the function in a variable for later reference.

    Code php:
    $x = sum($a, $b);

    Functions don't have a concept of 'fail' or succeed. A function that echos a bunch of text to the browser doesn't need to return anything.

    Since functions are operation aliases at heart, they can be "nested" - that is functions can take the returns of other functions as arguments.

    Code php:
    function double($a) {
      return $a*2;
    }
     
    $x = sum(double(6), double(2));

    When you do this keep in mind that functions near to the top of the order of operations as follows -- parenthesis, functions, exponents, multiplication, addition.

    If you do have a function that needs to convey an error state it can throw an exception - but this is an advanced topic for later.

    Most important thing to take from this -
    Do not get in the habit of having functions change variables that don't belong to them when they are called.

    Almost as important: Do not use global. It's a bad habit that will haunt you at the advanced level. For that matter do not use or worry with assign by reference yet. It just confuses the issue. And in my opinion assignment by reference in PHP causes more problems than it solves - the problem it is meant to solve doesn't even exist in PHP anyway so I don't use the darn thing.

    Finally, return is for giving the result of the function's operation to the calling code.

  19. #69
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Michael, really helpful advice. Nice to know that functions can actually work quite simply and that is perhaps their best way to use them.

    I wrote this just before I read your post.

    PHP Code:
    function sarnie($bread $filling$sauce){
        
        return 
    "$filling is nice with $sauce in a $bread bread sandwich";
        }
        
        echo 
    sarnie("white",  "Tuna""mayonaisse"); 
    I've noted from others and yourself that there is no real need to return if just displaying a string but I parsed some values into the variables just to try and see if I could do something that worked.


    I also tried this way which Im a bit unsure about.

    PHP Code:
    function sarnie2($bread "white"$filling "Tuna"$sauce"mayonaisse"){
        
        return 
    "$filling is nice with $sauce in a $bread bread sandwich";

    I'm not sure how to call the second one though, could someone help me with the syntax...?

  20. #70
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    For that function, what you are doing is setting default values for parameters that aren't passed to the function. So, if I call the function and set bread and filling, sauce will take on its default value because it wasn't passed.

    PHP Code:
    sarnie2('brown''cheese''ketchup')
    sarnie2('brown''cheese');
    sarnie2('brown');
    sarnie2(); 
    In the first of these cases, you are passing all 3 variables so the default values have no effect. In the second case, $sauce isn't passed, so it automatically assumes the value 'mayonaisse'. In the 3rd example, only $bread is set, so $filling and $sauce take on their default values. In the last example, nothing is set, so everything takes its default value.

    So you'd expect:

    Code:
    cheese is nice with ketchup in a brown bread sandwich
    cheese is nice with mayonaisse in a brown bread sandwich
    Tuna is nice with mayonaisse in a brown bread sandwich
    Tuna is nice with mayonaisse in a white bread sandwich

  21. #71
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,821
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Ethan-27 View Post
    Thanks for writing that mate. :-)

    Little bit confused though why the string could not just be echoed after the code of that function rather than writing another "if" statement... is there not a way to do that...?

    May I also ask why you returned both 'true' and 'false' in the 3rd function...?
    This may have been answered before...

    I wrote the code out for instructional purposes. Basically i wanted to show you what COULD be done.

    In programing there are two ways you can JUDGE someones work... functional and elegant. You can be code can be functional, working perfectly, and completely inelegant ( and flame worthy). Strive for elegant, even if you are a bit confused.

    so... to explain my rationale:
    1) wanted to show that you can have as many RETURN statements in a function as necessary.
    2) that if you want to have a function that checks if something sis true or false you need to return at least TRUE or FALSE ( the return statement by itself does nothing BUT escape the function, it doesn't actually send a value back UNLESS you tell it to)
    3) Yeah you COULD echo the statement in a function.. in fact some functions do nothing but ECHO things, but...
    4) ...it is more elegant and more logical, and also more flexible in the case i was out lining that the function merely checked.
    5) a fuction is a mini program that can be used as many times as needed. so, when devising functions try to make them as flexible for OTHER uses as possible... if I had echoed in the function then that means the function could only be used to say "yes its a number..." or that additional code would be needed to tell it that the echoing is not wanted at that particular instance... the way I wrote it I can use that function at any other time in the fututr when I need to check that something is a number...


    see.



    Oh and expounding on what StromR said...

    the another benefit of setting defaults, is that a function will throw an error if it doesn't get all the argument its expecting. Setting defaults at least prevents those errors ( tho it isnt always practical to set ALL your variables to default). You also have to remember that if you need to pad for defaults...

    what I mean by that is , well lets say you needed to change the filling and only the filling.

    sarnie2( 'ham').. will output:
    "Tuna is nice with mayonaisse in a ham bread sandwich"
    this is because the first argument always goes to the first default...
    so if you change is in the second argument for example.... you need to remember to pad ( that is to include preceding arguments)

    sarnie2( 'brown,'ham'); // the last argument doesn't need to be changed in this case...

  22. #72
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right ok nearly there lol. Once again guys thanks for all your patience and time. Im not sure there is a place in the world that can cover functions more than in this post, every angle seemed to have been covered.

    Today I'm gonna crack on and write some functions and look into why some functions have more than one return (as in phoenix example, cheers man :-) )

    Thanks again and like I said before, it's great to know so many people are willing to help when someone doesn't understand.

    Have a great day guys wherever you are

  23. #73
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Ethan-27 View Post
    I'm sure 'define; is confusing me somewhere in here aswell.

    PHP Code:
    function adding(){
        
    global 
    $moves;

    $moves $moves 10;

    }
    $moves 15;
    adding();
    echo 
    "$moves <br/>"
    If I was to remove global, '15' wouldn't be parsed...
    If you remove 'global', the variable $moves would be unknown inside the function.
    Code:
    function adding() {
      global $moves; // <-- (3)
      $moves = $moves * 10;
    }
    
    $moves = 15;  // <-- (1)
    adding(); // <-- (2)
    echo "$moves <br/>";
    (1) Here you define the variable $moves and give it the value 15. The fact that the function code is positioned before this line means nothing. The code inside the function (and therefore also the global) is executed only when the function is called (see (2)).
    (2) The function is called without passing the $moves value as an argument
    (3) Inside the function, $moves is undefined (out of scope) until you declare it global. From that moment on, this function (and only this one) is able to use the $moves variable you declared in (1).

    What you can not do is this (I remember making this mistake):
    Code:
    function adding() {
      $moves = $moves * 10;
    }
      
    global $moves = 15; 
    adding(); 
    echo "$moves <br/>";
    I thought this would make $moves global so it could be used everywhere. But that's not how it works.

  24. #74
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    New Zealand
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It might help you understand things by using this analogy:

    Think of a function as an expert at one thing.

    Let's call one expert Joe and another expert Jane.

    Joe is really good at adding numbers, so whenever you need to know the sum of two numbers, you ask Joe.
    PHP Code:
    //Joe function
    function addNumbers($first$second) {
        return 
    $first $second;

    Jane is really good checking lists to see if they contain a specific item, so when you want a list checked, you ask Jane.
    PHP Code:
    //Jane function
    function isInList($list$item) {
       if ( 
    in_array($item$list) ) {
          return 
    true;
       } else {
          return 
    false;
       }

    So who are you? You are the rest of the code in your application.

    So let's put this all together.

    As you (the application) are being processed, you come across the need to add 32+65. Instead of doing the math yourself, you call out to Joe, "Hey Joe, what's 32+65?" Joe yells back, "97," and you carry on processing.
    PHP Code:
    //Calling Joe
    $sumOfNumbers addNumbers(3265); //97 
    Then you call out to Jane, "Hey Jane, is that number in my list?" and she calls back, "No"
    PHP Code:
    //Calling Jane
    $myList = array(23571113);
    $inList isInList($myList$sumOfNumbers); //false 
    If Jane says the number is not in your list, you add it to the list.
    PHP Code:
    //Adding it to list
    if (!$inList) {
       
    $myList[] = $sumOfNumbers;//$myList = array(2, 3, 5, 7, 11, 13, 97);

    And check once more with Jane
    PHP Code:
    //Calling Jane again
    $inList isInList($myList$sumOfNumbers); //true 

  25. #75
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KiwiJohn View Post
    It might help you understand things by using this analogy:

    Think of a function as an expert at one thing.

    Let's call one expert Joe and another expert Jane.

    Joe is really good at adding numbers, so whenever you need to know the sum of two numbers, you ask Joe.
    PHP Code:
    //Joe function
    function addNumbers($first$second) {
        return 
    $first $second;

    Jane is really good checking lists to see if they contain a specific item, so when you want a list checked, you ask Jane.
    PHP Code:
    //Jane function
    function isInList($list$item) {
       if ( 
    in_array($item$list) ) {
          return 
    true;
       } else {
          return 
    false;
       }

    So who are you? You are the rest of the code in your application.

    So let's put this all together.

    As you (the application) are being processed, you come across the need to add 32+65. Instead of doing the math yourself, you call out to Joe, "Hey Joe, what's 32+65?" Joe yells back, "97," and you carry on processing.
    PHP Code:
    //Calling Joe
    $sumOfNumbers addNumbers(3265); //97 
    Then you call out to Jane, "Hey Jane, is that number in my list?" and she calls back, "No"
    PHP Code:
    //Calling Jane
    $myList = array(23571113);
    $inList isInList($myList$sumOfNumbers); //false 
    If Jane says the number is not in your list, you add it to the list.
    PHP Code:
    //Adding it to list
    if (!$inList) {
       
    $myList[] = $sumOfNumbers;//$myList = array(2, 3, 5, 7, 11, 13, 97);

    And check once more with Jane
    PHP Code:
    //Calling Jane again
    $inList isInList($myList$sumOfNumbers); //true 
    Dude I love examples like this!! Coding can become quite a slug sometimes but keeping it fun and interesting is the key to learning anything, thanks man.

    Once again big shout out to Guido aswell, I'm actually getting there now!!

    When it's clicked I will post some good functions on here. There is so much good information now that any noob getting stuck with this will find his answers here.


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
  •