0

I would like to do these actions step by step:

  1. first DB update
  2. copy file
  3. unlink file
  4. second DB update

It is working, but I don't know if my code is correct/valid:

$update1 = $DB->query("UPDATE...");

  if ($update1)
  {
    if (copy("..."))
    {
      if (unlink("..."))
      {
        $update2 = $DB->query("UPDATE ..."); 
      }         
    }
  } 

Is it possible to use if statement this way?

I found that it is usually used with PHP operators and PHP MySQL select, for example:

$select = $DB->row("SELECT number...");
  if ($select->number == 2) {
  ...
  }
2
  • 3
    if is used pretty much whenever there is conditional branching that needs to occur. It doesn't matter where the value comes from (and yes, SQL is one popular data source). In any case, see the documentation for whichever method to see what the results mean. You may also want to ensure that update2 "succeeded" and/or use a transaction or optimistic concurrency. Commented Oct 4, 2013 at 0:46
  • 2
    If you have working code and you want to improve it, consider asking on codereview.stackexchange.com Commented Oct 4, 2013 at 0:54

1 Answer 1

1

Sure, your ifs work fine. What would look and flow better would be using a function like this:

function processThings() {
    // make sure anything you use in here is either passed in or global

    if(!$update1)
        return false;

    if(!$copy)
        return false;

    if(!$unlink)
        return false;

    if(!$update2)
        return false;

    // you made it!
    return true;
}

make sure you call $DB as a global variable, plus pass in whatever strings you need etc etc

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.