The ‘||’ operators and ‘or’

Good time.

Now I was working with the database and I was puzzled by the question:

$db_res = sqlite_open( $db_name ) || die ("   !" );
$query = sqlite_query($db_res, "SELECT * FROM $table") || die ('  !');

I get an error when using “|| die()”

Warning: sqlite_query() expects parameter 1 to be resource, string given in ... on line ...
!

If you change the operator “||” on “or” everything works correctly, why?


Answer 1, authority 100%

This is because the ||operator has a higher precedencethan the =, so $db_reswill be set to the result of sqlite_open( $db_name ) || die (" !" ), i.e. true.

The oroperator, on the contrary, has prioritythan =, therefore, $db_reswill first be assigned the result of sqlite_open( $db_name )and only then will the logical OR be applied: $db_res || die (" !" )

I.e. if you explicitly prioritize, then both expressions will look like this:

$db_res = ( sqlite_open( $db_name ) || die ("   !" ) );
( $db_res = sqlite_open( $db_name ) ) or die ("   !" );

Answer 2

And programming conventions say that OR and AND must be used before || and &&. So I advise you to use the alphabetic version.