I thought I could call a function twice in PHP

by hometutor 9 replies
Newbie here,

I thought I had a solution to my looping problem

function foo($a,$b) {

do stuff

}

$a='blah'
$b='yuck'

foo($a,$b)

// but I cannot run it a second time with the same variables

$a='yum'

$b='mmm'

foo($a,$b)

//The function will not run the second time around. Is this a php rule I haven't run across yet?

Thanks

Rick
#programming #call #function #php #thought
Avatar of Unregistered
  • Profile picture of the author mojojuju
    There's nothing that necessarily says you can't use a function twice in PHP. I'm wondering what kinds of stuff is in your function and I'm also wondering if there's something in your program that's preventing the function from being called a second time.
    Signature

    :)

    {{ DiscussionBoard.errors[10151688].message }}
  • Profile picture of the author h4x0r
    it should run perfectly without an issue, can you post your code not pseudo code maybe it has an error in it?
    {{ DiscussionBoard.errors[10151800].message }}
    • Profile picture of the author hometutor
      Originally Posted by h4x0r View Post

      it should run perfectly without an issue, can you post your code not pseudo code maybe it has an error in it?
      Too long and sloppy

      Works in the first call works in the second call, but not both function calls. Also works with include. Might just be too big.

      Rick
      {{ DiscussionBoard.errors[10151841].message }}
      • Profile picture of the author KirkMcD
        If you couldn't call a function more than once, there would be no point in having functions.

        Originally Posted by hometutor View Post

        Too long and sloppy
        <snip> Might just be too big.
        It cant be too big.

        If it's "too long and sloppy," you probably have errors in it.

        Are you using global variables in it?
        Are you opening files?
        {{ DiscussionBoard.errors[10159471].message }}
        • Profile picture of the author hometutor
          Originally Posted by KirkMcD View Post

          If you couldn't call a function more than once, there would be no point in having functions.



          It cant be too big.

          If it's "too long and sloppy," you probably have errors in it.

          Are you using global variables in it?
          Are you opening files?
          Yes opening files and using global vars, but I set the vars in the function as well since I read a global var wouldn't pass to a function. Interesting that it works as an insert, but not a function. The counter's an interesting thought.

          Rick
          {{ DiscussionBoard.errors[10160270].message }}
          • Profile picture of the author KirkMcD
            Originally Posted by hometutor View Post

            Yes opening files and using global vars, but I set the vars in the function as well since I read a global var wouldn't pass to a function.
            Do you close the files?

            To use a global variable in a function you need to declare them in the function;
            Ex:
            function foo($a,$b)
            global $c, $d
            ...
            {{ DiscussionBoard.errors[10161006].message }}
            • Profile picture of the author hometutor
              Originally Posted by KirkMcD View Post

              Do you close the files?

              To use a global variable in a function you need to declare them in the function;
              Ex:
              function foo(,)
              global ,
              ...
              Yes I did close the files after an initial attempt. The vars were declared I'm guessing it wouldn't have run once if I had not.

              Rick
              {{ DiscussionBoard.errors[10161550].message }}
  • Profile picture of the author PHR
    Hi there,

    if the second variable initialization value is constant, I would suggest you make a "recursive function"!

    Just declare and initialize it in your too method one the main part of the function is executed successfull and then call it again, like:

    //global counter variable...

    $counter = 0;

    function doSmth($a,$b) {



    //code to be executed here...

    $a = '1';
    $b = '2';

    if($counter < 1) {


    doSmth($a, $b);

    $counter++;

    }
    }

    Hope I could help you out a little bit.

    Regards,
    Peter
    {{ DiscussionBoard.errors[10159059].message }}
    • Profile picture of the author David Beroff
      Originally Posted by PHR View Post

      I would suggest you make a "recursive function"!
      With all due respect, Sir, I disagree with this suggestion, especially for someone who is just learning the ropes. Recursive functions have great utility for very specific problems which are recursive in nature, but the vast majority of problems are not. (e.g., I recently used this approach to display an affiliate genealogy tree, where referrers have referrers have referrers....)

      Yes, yes, I know that recursion is taught in school, but rarely is the solution presented in conjunction with the types of situations where it should actually be used.

      My main concern is that someone who is just starting out, who's having difficulty in calling a function twice, or starting to learn about boolean logic and parentheses, can accidentally create far more problems than recursion might ever solve.
      Signature
      Put MY voice on YOUR video: AwesomeAmericanAudio.com
      {{ DiscussionBoard.errors[10160496].message }}
Avatar of Unregistered

Trending Topics