Using if statement in PHP I would like to do these actions step by step:


first DB update
copy file
unlink file
second DB update


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

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

  if ($update1)
  {
    if (copy("..."))
    {
      if (unlink("..."))
      {
        $update2 = $DB-&gt;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-&gt;row("SELECT number...");
  if ($select-&gt;number == 2) {
  ...
  }
Is it possible to use if statement this way?


Yes.

Whether it is a good style depends on other factors.

Let us look at a few alternatives. First, your code (with cuddled braces):

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


Then the same code using logical AND (&amp;&amp;).

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


And another one without the braces {};

if ( $update1 &amp;&amp; copy( "..." ) &amp;&amp; unlink( "..." ) )
    $update2 = $DB-&gt;query( "UPDATE ..." );


And finally, an example that doesn't use if.

$update1
    &amp;&amp; copy( '...' )
    &amp;&amp; unlink( '...' ) )
    &amp;&amp; $update2 = $DB-&gt;query( "UPDATE ..." );


All these examples perform the same operations.

If the ... stuff is long, the latter methods can get very wide conditionals making it difficult to read the code to see what is going on.

if ( $update1 &amp;&amp; copy( 'a/very/long/source/path', 'a/very/long/destination/path' ) &amp;&amp; unlink( 'a/very/long/source/path' ) )
    $update2 = $DB-&gt;query( "UPDATE ..." );


You could rewrite it like this:

if (   $update1
    &amp;&amp; copy( 'a/very/long/source/path', 'a/very/long/destination/path' )
    &amp;&amp; unlink( 'a/very/long/source/path' ) )
    $update2 = $DB-&gt;query( "UPDATE ..." );


But it looks little clunky. It is hard to see where the conditional ends.

The last style makes this prettier:

$update1
    &amp;&amp; copy( 'a/very/long/source/path', 'a/very/long/destination/path' )
    &amp;&amp; unlink( 'a/very/long/source/path' ) )
    &amp;&amp; $update2 = $DB-&gt;query( "UPDATE ..." );


I do not care for this style, but it looks pretty and it tells you exactly what is happening at a glance.

One problem with most of the alternatives to the first method is debugging and maintenance.

If I want to log the unlink error is some special way I can do this in the first method:

if ( $update1 ) {
    if ( copy( "..." ) ) {
        if ( unlink( "..." ) ) {
            $update2 = $DB-&gt;query( "UPDATE ..." );
        } else {
            log_unlink_error( '...' );
        }
    }
}


To quickly add that code to the other methods described here, may require more rewriting.

So, in this case, your style for handling multiple conditions might depend on the age of your code and how much testing you expect to be doing.


If it is well tested and you never need to edit it, then one of the alternatives is fine if you use that style consistently throughout all your code.
If this is new code, write it out the long way. It may be easier to test and maintain.