-1

I have a php script to pipe through a mail,

class mailTest
{
    // (some code here)
    private function saveToDb()
    {
        // (some code here)
        $select = $this->pdo->query("SELECT * FROM tbl_reques WHERE terminal_id = $term AND request_status ='' ");
        $select = $select->fetchAll();
        if (count($select) > 0) {
            echo "Call already Exist (DISCARD)";
        } else {
            $select_tech = $this->pdo->query("SELECT * FROM tbl_email WHERE terminal_id = $term");
            $select_tech = $select_tech->fetchAll();
            // (some code here)
        }
    }

    private function sendEmail()
    {
        $this->today = time();
        $this->maildate = date("Y-m-d H:i:s", strtotime('-5 minutes', $this->today));

        $select = $this->pdo->query("Select * from tbl_reques WHERE maildate >=  '$this->maildate' ");
        // some code here
        mail($this->from_email, $this->subject, $newmsg, $headers);
    }
}

The problem is any time the condition is False i.e echo "Call already Exist (DISCARD)"; The code will not go to the Next Function. i.e the program get halt. PLS is there a way that if that condition is not met, the program will JUMP to next function for continuation of execution. Or is it possible to use GOTO statement. Pls what is the best way to handle this in PHP. Thanks

JSchirrmacher
  • 3,243
  • 2
  • 19
  • 27
Steve
  • 21
  • 2
  • 9
  • 1
    no, there is no jump, and you should never be using `goto`. if you want a function to terminate early, then have it `return`. and if you want the next function to execute immediately, then call it immediately: `foo(); bar();` – Marc B Jul 21 '16 at 18:50
  • Returning in the middle of a function is no better than using goto. I don't know why that terrible pattern has so much traction in so many languages. – alzee Jul 21 '16 at 19:03

2 Answers2

1

You have a couple option for this. You can return at the point of failure. Which would exit the function at this point and then do whatever is next in the script being ran. Be sure to do the clean up before your return.

if(count($select) > 0) {
    echo "Call already Exist (DISCARD)";
    //Clean up if needed
    return; //You could also return the message 
            //or an error code and have another 
            //evaluation based on that.
} else {
    // Or passes
}

You could call the next function but this would be a very bad flow in my opinion

if(count($select) > 0) {
    echo "Call already Exist (DISCARD)";
    //Clean up if needed
    $this->sendEmail();
} else {
    // Or passes
}

The reason this would be bad is if say in the script you have

$mailTest = new mailTest();
$mailTest->saveToDb();
$mailTest->sendEmail(); //When the above fails this is called twice.

You could likewise throw an exception

if(count($select) > 0) {
    echo "Call already Exist (DISCARD)";
    throw new Exception("Call already Exist (DISCARD)");
} else {
    // Or passes
}

Now you need to use try and catch

$mailTest = new mailTest();
try {
    $mailTest->saveToDb();

catch (Exception $e){
    //Do something with $e
    //Clean up the failure if needed
}
$mailTest->sendEmail();

There is a finally block as well which would run in cases where your catch stops the script.

nerdlyist
  • 2,842
  • 2
  • 20
  • 32
0

PHP in fact has a GOTO statement, see http://php.net/manual/de/control-structures.goto.php

However, it is considered bad style to use it, or in the words of @Konamiman

Unless you are programming in assembler, GOTO should always be treated the same way as the life vest of the airplanes: it is good to have them available, but if you need to use them it means that you are in big trouble.

You can call a function simply by writing its name followed by brackets. In the case of class functions you apply it to $this:

$this->sendEmail();
Community
  • 1
  • 1
JSchirrmacher
  • 3,243
  • 2
  • 19
  • 27