i just want to know from below code what does ?
and :
specify, i would appreciate if someone explain me the below code. thank you
$country = empty($_POST['country']) ? die ("ERROR: Enter a country") : mysql_escape_string($_POST['country']);
i just want to know from below code what does ?
and :
specify, i would appreciate if someone explain me the below code. thank you
$country = empty($_POST['country']) ? die ("ERROR: Enter a country") : mysql_escape_string($_POST['country']);
It is called ternary operator and is simply shorthand of this code:
if (empty($_POST['country']))
{
die ("ERROR: Enter a country");
}
else
{
$country = mysql_escape_string($_POST['country']);
}
Syntax:
condition ? used if true : used if false;
Or you can do assignments:
variable = condition ? used if true : used if false;
More Info:
See this:
PHP syntax question: What does the question mark and colon mean?
It is the ternary operator in PHP as well as other languages.
$country = empty($_POST['country']) ? die ("ERROR: Enter a country") :
I suppose this script accepts data from a form sent by POST method. If country variable is empty, exit the script with error message.
mysql_escape_string($_POST['country']);
This function should return escaped value from given variable. Therefore it should be written like this
$country = mysql_escape_string($_POST['country']);
More info here: http://php.net/manual/en/function.mysql-escape-string.php
$country = empty($_POST['country']) ?
die ("ERROR: Enter a country") :
mysql_escape_string($_POST['country']);
If the expression empty($_POST['country'])
evaluates to true
, then die ("ERROR: Enter a country")
will be evaluated (and the result would be assigned to $country
, but for the fact that die()
halts script execution).
On the other hand, if empty($_POST['country'])
evaluates to false
, then mysql_escape_string($_POST['country'])
will be evaluated, and the result will be assigned to $country
.
It's test condition: if variable from HTML FORM is empty then print out "ERROR: Enter a country", else set variable country safe characters..