And as a final note, to address your original question of:
Quote:
|
Originally Posted by LearningNerd
I'm also confused about how they're using "or die(...)" -- you can set variables equal to "function() or function()"? I've never seen this before!
|
Or is an operator, and it's what we call `short-circuiting' operator. Basically, saying `true or false' will return true, but it will return true without even *looking* at false, because you don't need to -- the result of an or is true if *any* of its parts are true.
Now, in PHP, `null' is equivalent to `false'. So, if I say `true or null', we get the same result (again, because we only look at true), and if I say `null or true', we also get `true'. Typically, anything that *isn't* null or false or 0 (I believe those are all of the false values in PHP) evaluates as `true' for the purposes of ors or conditionals. Moreover, what `or' does isn't return `true' or `false'. It returns the first value that doesn't evaluate to false. So `5 or false' would return 5, `6 or 7 or 8 or 9' would return `6', and so on.
Progressing to the next step, mysql_connect will return a null pointer, or a null value, when it fails to connect. We just mentioned that `or' won't process anything past the first value that evaluates to not-false. So, when we do this:
PHP Code:
mysql_connect( /* ... */ ) or die( "Got an error, stopping script..." )
We know that if mysql_connect returns something that isn't null (i.e., it succesfully connects), then die() won't even run, and the value of that entire expression will be the value returned by mysql_connect.
Similarly, when mysql_connect returns null (i.e., it fails to connect), then die() will run, and it will kill the output with the error message you pass it, and execution of the PHP will stop there.
Hope that made sense