Jump to content

($con) and (!$con)?


eduard

Recommended Posts

if ($con) {statement} will execute {statement} if $con has a value that evaluates to true.if (!$con) {statement} will execute {statement} if $con has a value that evaluates to false.$con can contain any data type. Many developers use $con to hold the result of mysql_connect. if ($con) is a way of testing whether the connection to a database was successful or not.

Link to comment
Share on other sites

if ($con) {statement} will execute {statement} if $con has a value that evaluates to true.if (!$con) {statement} will execute {statement} if $con has a value that evaluates to false.$con can contain any data type. Many developers use $con to hold the result of mysql_connect. if ($con) is a way of testing whether the connection to a database was successful or not.
Thanks very much! However, is there no other way than $con to check if you have connection or not?
Link to comment
Share on other sites

You can name the variable anything you want. The if-structure is the normal way to test your connection. You could replace it with the ternary operator ( ?: ), but I don't see a good reason for doing so.

Link to comment
Share on other sites

When do you use: ($con) and when (!$con)?
it's more important to know what each one is doing. although you didn't provide it, I assume the context of this usage is in an if or if/else statement, which test a condition (what you put between the parenthesis) to find out whether it evaluates to true or not (false). What this does:
if($con){   //code};

is test's if $con evaluates to true, and if it does, then the code within the curly braces will execute.when used like this:

if(!$con){  //code};

the introduction of the ! exclamation point now asks for the if statement to test for false, because the ! is commonly used to test for inequality (false). So now if $con evaluates to false, the code in the curly braces will execute. So if a mysql_connect result comes back as not being able to connect, then $con would equal false (as dictated by the return value of mysql_connect) and that's why it's used like this:

$con = mysql_connect("localhost","mysql_user","mysql_pwd");if (!$con){  die('Could not connect: ' . mysql_error());};

Link to comment
Share on other sites

it's more important to know what each one is doing. although you didn't provide it, I assume the context of this usage is in an if or if/else statement, which test a condition (what you put between the parenthesis) to find out whether it evaluates to true or not (false). What this does:
if($con){   //code};

is test's if $con evaluates to true, and if it does, then the code within the curly braces will execute.when used like this:

if(!$con){  //code};

the introduction of the ! exclamation point now asks for the if statement to test for false, because the ! is commonly used to test for inequality (false). So now if $con evaluates to false, the code in the curly braces will execute. So if a mysql_connect result comes back as not being able to connect, then $con would equal false (as dictated by the return value of mysql_connect) and that's why it's used like this:

$con = mysql_connect("localhost","mysql_user","mysql_pwd");if (!$con){  die('Could not connect: ' . mysql_error());};

Thanks! This is what others wrote me!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...