stelios Posted January 15, 2007 Share Posted January 15, 2007 Hi all,I think this is a pretty basic question, too basic probably... :-[, but here is goes.Lets say I have:[b]Function A[/b], returns a value[b]Function B[/b], doesn't return a value.If [b]function A[/b] calls [b]function B[/b], will [b]function A[/b] have to wait until [b]function B[/b] terminates or it will continue execution seamlessly?ThanksStelios Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted January 15, 2007 Share Posted January 15, 2007 Function A will wait for function b to terminate. You can convince yourself of this by running the following trivial code.[code]<?phpfunction a(){ b(); return "a";}function b(){ echo "b";}echo a();// Will print "ba"?>[/code] Quote Link to comment Share on other sites More sharing options...
stelios Posted January 15, 2007 Author Share Posted January 15, 2007 First of all thanks for the quick answer.So is there a way to call a function from within another one and not wait till it finishes execution?stelios Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted January 15, 2007 Share Posted January 15, 2007 If I understand you correctly you're asking if you can do something like this:[code]<?phpfunction A(){ B(); return $somthing;}function B(){ while(true){ // Do something here in parallel to what's going on in the calling function. }}?>[/code]If so, you're looking to write a multi threaded program. PHP does not handle this natively, you can probably hack your way through something on a *nix system outside of a webserver using forked processes but it won't be pretty. My suggestion would be to take a look at Java as it supports multi threading natively.Best,Patrick Quote Link to comment Share on other sites More sharing options...
stelios Posted January 15, 2007 Author Share Posted January 15, 2007 ok to explain better the situation is like this:I have a registration/activation group of functions that obviously send an email to activate the account when the user registers. What I want is to handle the case that the email is failed to be sent. So I thought of having another function running that lets say retries to send the email 10 times and if it fails it registers that into a DB table. Since that is not possible with php my thought is in case of email failure register that in DB, and have another script(shell script?) called that will retry to send the email x times. All this is in the purpose of not hacing the user wait all the time it takes to resend the email.thanks for your timestelios Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted January 15, 2007 Share Posted January 15, 2007 Are you sending email via PHP's mail function? If so, how are you detecting that the email was not sent? Quote Link to comment Share on other sites More sharing options...
stelios Posted January 15, 2007 Author Share Posted January 15, 2007 well it returns false on failure. Although I don't know if he received it I can at least tell if the function succeded... Quote Link to comment Share on other sites More sharing options...
trq Posted January 16, 2007 Share Posted January 16, 2007 [quote]Since that is not possible with php[/quote]It is possible with php, but the whole process will need to wait until its finished. eg;[code]<?php function send_email($email) { for ($i=1,$i=10,$i++) { if (mail('blah blah')) { return true; } } return false; }?>[/code]This will attempt to send the email up to 10 times. It will return true as soon as it succeeds or false if it never does. However.... I would not recommend actually using this apprough. Just because mail returns true / false does not meen the email was successfully recieved. Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted January 16, 2007 Share Posted January 16, 2007 I guess my question is, if the mail function fails the first n times. Why would we expect it to work the (n + 1)'th time or the (n + 100)'th time for that matter? Furthermore is this something that will happen enough to warrant fixing the prolbem programatically or would it be enough to flag the problem (which you are already doing) and resending the email in batch at another time? Quote Link to comment Share on other sites More sharing options...
btherl Posted January 16, 2007 Share Posted January 16, 2007 I would go with the approach of flagging it in the database and having another script try again later. It's probably the least complex, as you don't have to worry about holding up the user. Quote Link to comment Share on other sites More sharing options...
stelios Posted January 16, 2007 Author Share Posted January 16, 2007 Reply to: thorpe | Reply #7I know I can do that, but that is what I want to avoid...keeping the user waiting.Reply to: utexas_pjm | Reply #8[quote]or would it be enough to flag the problem (which you are already doing) and resending the email in batch at another time?[/quote]This is what I'm planning to do. In general I can not verify that the email will be sent since it wasn't sent on the first time, but I want to resend it as a first measure and then flag it accordingly for further investigation.Reply to: btherl | Reply #9This is the approach I'm testing now. Writing a script perl at the moment. Will let you know how it goes.Thanks for the help guysstelios Quote Link to comment Share on other sites More sharing options...
stelios Posted January 16, 2007 Author Share Posted January 16, 2007 yep calling perl form php works:)PHP doesn't have to wait for the perl script to finish execution.Although this approach might not be the most efficient. I think "utexas_pjm | Reply #8", having a batch file might be the best idea. Although I will keep running the perl script for now, see how many emails fail and/or if the resend succeds.very nice to see such an active forum. thanks for the help and I hope I can contribute to the forum at some point...:)stelios Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.