| | #1 |
| Knowledge is power War Room Member Join Date: 2004 Location: Honolulu,Hawaii , USA.
Posts: 1,354
Thanks: 406
Thanked 243 Times in 191 Posts
|
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 |
| | |
| |
| | #2 |
| VIP Warrior Join Date: 2005 Location: Potemkin Village
Posts: 1,724
Thanks: 745
Thanked 742 Times in 498 Posts
|
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.
|
|
:)
| |
| |
| | #3 |
| Warrior Member War Room Member Join Date: 2012
Posts: 16
Thanks: 0
Thanked 3 Times in 3 Posts
|
it should run perfectly without an issue, can you post your code not pseudo code maybe it has an error in it?
|
| |
| | #4 |
| Knowledge is power War Room Member Join Date: 2004 Location: Honolulu,Hawaii , USA.
Posts: 1,354
Thanks: 406
Thanked 243 Times in 191 Posts
| |
| | |
| |
| | #5 |
| HyperActive Warrior Join Date: 2012
Posts: 354
Thanks: 56
Thanked 43 Times in 36 Posts
|
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 |
| |
| The Following User Says Thank You to PHR For This Useful Post: |
| | #6 |
| VIP Warrior War Room Member Join Date: 2006 Location: , , USA.
Posts: 1,795
Thanks: 70
Thanked 287 Times in 259 Posts
|
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? |
| |
| The Following 2 Users Say Thank You to KirkMcD For This Useful Post: |
| | #7 | |
| Knowledge is power War Room Member Join Date: 2004 Location: Honolulu,Hawaii , USA.
Posts: 1,354
Thanks: 406
Thanked 243 Times in 191 Posts
|
Rick | |
| | ||
| |
| | #8 |
| Bananas are Yellow! Registered Member War Room Member Join Date: 2013 Location: Wyoming, PA, USA
Posts: 1,046
Thanks: 2,549
Thanked 1,600 Times in 928 Posts
Blog Entries: 4 |
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. |
| Put MY voice on YOUR video: AwesomeAmericanAudio.com | |
| |
| | #9 | |
| VIP Warrior War Room Member Join Date: 2006 Location: , , USA.
Posts: 1,795
Thanks: 70
Thanked 287 Times in 259 Posts
|
To use a global variable in a function you need to declare them in the function; Ex: function foo($a,$b) global $c, $d ... | |
| |
| | #10 |
| Knowledge is power War Room Member Join Date: 2004 Location: Honolulu,Hawaii , USA.
Posts: 1,354
Thanks: 406
Thanked 243 Times in 191 Posts
| |
| | |
| |
|
| Bookmarks |
| Tags |
| call, function, php, thought |
| |