/    Sign up×
Community /Pin to ProfileBookmark

Simultaneous Functions Help

PHP Pro’s I need help.

Is there a way to only execute a function once another function has been executed?

For security reasons I cannot show you my code but I can give you an example of what I am doing.

[code=php] function mailForm($elements){
/*do something*/
header(‘Location: blah.php’);
}

if(isset($_POST[‘form’]) && $_POST[‘form’]==1) {
$elements = array($_POST[‘something’]);
mailForm($elements);

/*In the function above there is a script that returns you to a page if an error is found in the form validation*/

/*However because the PHP scripts execute the functions at the same time (I think!) it does not take you back to the page until it has executed the following function*/

$elementsPartTwo = array($_POST[‘something’]);
mailForm($elementsPartTwo);
}[/code]

I think that this is a very simple problem and I am just being stupid but everything I have tried has not worked.

Thanks in Advance,

Ben

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 24.2010 — Two things, both of which you might want to do to cover all bases.

  • 1. In the first function, do an [b]exit;[/b] right after the [b]header()[/b] call, so that nothing else gets processed. This is probably all you really need for this particular case. (It's not an issue of simultaneity, but rather that header() only adds that HTTP header to the queue, so that it gets sent with the rest of the headers sent by the web server. It does not stop the script from continuing to process subsequent commands.)


  • 2. As a general rule, it's a good idea to have any function [B]return[/B] [i]something[/i], so that the calling code can determine what happened. It could be a Boolean true or false, a numeric value, or whatever makes sense. Then when you call that function, you can check the return value and determine what should happen next as a result.
  • Copy linkTweet thisAlerts:
    @eval_BadCode_Sep 25.2010 — exit; and die('message before exiting');

    are helpful when you want to stop a page.


    Is there a way to only execute a function once another function has been executed?

    function1();

    function2();

    infact function2() can not execute unless function1() does. If you want to prevent function2() from executing, then you can stop the php script all togeather with die("text"); or exit; .


    more complicated?
    [code=php]
    <?php
    function function1($value) {
    $value = $value + " something";
    $also = "functions apply to the rules of scope";
    return $value;
    }

    function function2() {
    echo function1("im passing values around functions or");
    echo $also;

    }

    function2();
    ?>
    [/code]
    ×

    Success!

    Help @Benji6996 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...