Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 4th Jul 2015, 11:08 PM   #1
Knowledge is power
War Room Member
 
hometutor's Avatar
 
Join Date: 2004
Location: Honolulu,Hawaii , USA.
Posts: 1,354
Thanks: 406
Thanked 243 Times in 191 Posts
Default
I thought I could call a function twice in PHP
Share on: 
fb share twitter share gplus share more share

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

hometutor is offline  
Unread 5th Jul 2015, 12:34 AM   #2
VIP Warrior
 
mojojuju's Avatar
 
Join Date: 2005
Location: Potemkin Village
Posts: 1,724
Thanks: 745
Thanked 742 Times in 498 Posts
Default
Re: I thought I could call a function twice in PHP
Share on: 
fb share twitter share gplus share more share

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.

:)
mojojuju is offline  
Unread 5th Jul 2015, 02:52 AM   #3
Warrior Member
War Room Member
 
Join Date: 2012
Posts: 16
Thanks: 0
Thanked 3 Times in 3 Posts
Default
Re: I thought I could call a function twice in PHP
Share on: 
fb share twitter share gplus share more share

it should run perfectly without an issue, can you post your code not pseudo code maybe it has an error in it?
h4x0r is offline  
Unread 5th Jul 2015, 03:48 AM   #4
Knowledge is power
War Room Member
 
hometutor's Avatar
 
Join Date: 2004
Location: Honolulu,Hawaii , USA.
Posts: 1,354
Thanks: 406
Thanked 243 Times in 191 Posts
Default
Re: I thought I could call a function twice in PHP
Share on: 
fb share twitter share gplus share more share

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

hometutor is offline  
Unread 9th Jul 2015, 03:09 AM   #5
PHR
HyperActive Warrior
 
PHR's Avatar
 
Join Date: 2012
Posts: 354
Thanks: 56
Thanked 43 Times in 36 Posts
Default
Re: I thought I could call a function twice in PHP
Share on: 
fb share twitter share gplus share more share

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
PHR is offline  
The Following User Says Thank You to PHR For This Useful Post:
Unread 9th Jul 2015, 08:23 AM   #6
VIP Warrior
War Room Member
 
Join Date: 2006
Location: , , USA.
Posts: 1,795
Thanks: 70
Thanked 287 Times in 259 Posts
Default
Re: I thought I could call a function twice in PHP
Share on: 
fb share twitter share gplus share more share

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?
KirkMcD is offline  
The Following 2 Users Say Thank You to KirkMcD For This Useful Post:
Unread 9th Jul 2015, 05:16 PM   #7
Knowledge is power
War Room Member
 
hometutor's Avatar
 
Join Date: 2004
Location: Honolulu,Hawaii , USA.
Posts: 1,354
Thanks: 406
Thanked 243 Times in 191 Posts
Default
Re: I thought I could call a function twice in PHP
Share on: 
fb share twitter share gplus share more share

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

hometutor is offline  
Unread 9th Jul 2015, 08:35 PM   #8
Bananas are Yellow!
Registered Member
War Room Member
 
David Beroff's Avatar
 
Join Date: 2013
Location: Wyoming, PA, USA
Posts: 1,046
Thanks: 2,549
Thanked 1,600 Times in 928 Posts
Blog Entries: 4
Default
Re: I thought I could call a function twice in PHP
Share on: 
fb share twitter share gplus share more share

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.

Put MY voice on YOUR video: AwesomeAmericanAudio.com
David Beroff is offline  
Unread 10th Jul 2015, 04:10 AM   #9
VIP Warrior
War Room Member
 
Join Date: 2006
Location: , , USA.
Posts: 1,795
Thanks: 70
Thanked 287 Times in 259 Posts
Default
Re: I thought I could call a function twice in PHP
Share on: 
fb share twitter share gplus share more share

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
...
KirkMcD is offline  
Unread 10th Jul 2015, 10:54 AM   #10
Knowledge is power
War Room Member
 
hometutor's Avatar
 
Join Date: 2004
Location: Honolulu,Hawaii , USA.
Posts: 1,354
Thanks: 406
Thanked 243 Times in 191 Posts
Default
Re: I thought I could call a function twice in PHP
Share on: 
fb share twitter share gplus share more share

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

hometutor is offline  
Closed Thread


Bookmarks

Tags
call, function, php, thought


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 02:34 PM.