5
$conn = mysql_connect($db_host,$db_user,$db_password) || die('Connection to mysql failed');
mysql_close($conn);

On executing this code, the following warning was displayed.

PHP Warning: mysql_close() expects parameter 1 to be resource, boolean given in script.php on line 45

$conn = mysql_connect($db_host,$db_user,$db_password) or die('Connection to mysql failed');
mysql_close($conn);

No Warning Now!?

Yash
  • 929
  • 9
  • 25
  • 1
    Don't use mysql functions -.- – jeremy Jan 07 '14 at 07:23
  • 2
    @Jeremy That's not a terribly helpful comment. I doubt he's using the `mysql` library out of spite, he probably just doesn't know there's any reason not to. At the very least [link to some resources](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1) and help him learn. – Chris Hayes Jan 07 '14 at 07:35
  • @ChrisHayes I got lazy, though you're right. – jeremy Jan 07 '14 at 07:38
  • possible duplicate of [The behaviour of the or operator in PHP](http://stackoverflow.com/questions/12213283/the-behaviour-of-the-or-operator-in-php) – deceze Jan 07 '14 at 09:09
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Jan 07 '14 at 12:53

2 Answers2

9

&& and || are logical operators -- they're for Boolean conditional statements. As @towr and @ChrisHayes point you can use and and or in place of the && and || syntax, albeit at a lower precedence than most other operators.

or in this context, however, is completely different -- it's part of the control flow (see section 2.5.7: exit and return).

Boolean condition:

if ($foo == $bar || $bar != $bob)

Control flow:

mysql etc... or die();

You're receiving the error "PHP Warning: mysql_close() expects parameter 1 to be resource..." because you're not actually writing correct syntax, thus breaking your mysql_connect statement.

brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • 2
    Best answer but should address precedence even though the post doesn't ask about it and mentioning precedence wouldn't be a direct answer to the question. – jeremy Jan 07 '14 at 07:21
  • 4
    According the the documentation http://www.php.net/manual/en/language.operators.logical.php `or` and `and` are also just logical operators, but they have difference precedence than `||` and `&&`. – towr Jan 07 '14 at 07:22
  • Also, could you cite your sources? – jeremy Jan 07 '14 at 07:23
  • 3
    Deleted my own answer for being somewhat misleading, but I think @towr's point needs to be addressed more prominently here. `and` and `or` are *not* part of the control flow, they're simply short-circuiting logical operators which work at a lower precedence than most other operators. – Chris Hayes Jan 07 '14 at 07:29
  • @ChrisHayes agreed - not sure why I included `and` in there in the first place. – brandonscript Jan 07 '14 at 07:33
  • 1
    You _can_ also do the same using `||`, to wit `($conn = mysql_connect(...) ) || die(":(")` So, `or` is not special with regards to flow control, though it's easier to use in this context. But I think for a complete answer, it should be pointed out what both statements actually assign to $conn. – towr Jan 07 '14 at 08:14
  • The only difference between `||` and `or` is precedence. This difference in precedence has the effect of having a different outcome of `$foo = $bar || $baz` vs. `$foo = $bar or $baz`. `or` has in no way anything to do with "control flow". – deceze Jan 07 '14 at 09:05
5

and and or have higher lower precedence than && and ||. To be more exact && and || have higher precedence than assignment operator ( = ) while and and or have lower.

http://www.php.net/manual/en/language.operators.precedence.php

Usually it doesn't make a difference, but there are cases when not knowing about this difference can cause some unexpected behaviour. See examples here:

http://php.net/manual/en/language.operators.logical.php

EDIT(suggested by @towr):

Applied to the question at hand, this means that in the first case we assign to $conn the value mysql_connect(....) || die('....'),because || has a higher precendence than =. The problem here is that $conn now is a boolean, and not a resource.

In the second case we OR the expressions $conn = mysql_connect(....) and die('....'), because = has a higher precendence than OR. We do nothing with the boolean value, and $conn is simply the resource we assigned to it in the first expression (if it didn't fail).

Aks
  • 1,567
  • 13
  • 23
  • 3
    @Jeremy It does, because this means `$foo = $a || $b` equals `$foo = ($a || $b`) equals `$foo = ($a or $b`) whereas `$foo = $a or $b` equals `($foo = $a) || $b` equals (`$foo = $a) or $b` – towr Jan 07 '14 at 07:27
  • Hmm... so apply this to the OP's question. How does it answer it? – jeremy Jan 07 '14 at 07:29
  • @Aks There you go. +1 – jeremy Jan 07 '14 at 14:09