tags:

views:

90

answers:

5

hi,

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']); 
+1  A: 

See this:

http://stackoverflow.com/questions/1276909/php-syntax-question-what-does-the-question-mark-and-colon-mean-closed

It is the ternary operator in PHP as well as other languages.

Bob Fincheimer
+1  A: 

$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

dwich
+3  A: 

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:

http://www.tuxradar.com/practicalphp/3/12/4

Sarfraz
thank you i was expecting an answer like this, i am not used to shorthand.. thank you for this reply.. :)
Ibrahim Azhar Armar
Good answer, +1, but you forgot to add `$country = ` to the `else` block
Igor Zinov'yev
@Igor Zinov'yevL Yes forgot to add that, thanks :)
Sarfraz
For more information, here's the syntax of the ternary operator : condition ? value if true : value if false
Elenaher
Hi there,in the else condidtion i want to echo something like a link to go back to index page, i tried using concatenation and it is not happening.. check the last string$user = empty($_POST['user']) ? die("Error : Please enter username") : mysql_escape_string($_POST['user']) . echo "$_SERVER['PHP_SELF']";how do i achieve this??
Ibrahim Azhar Armar
First off, in this case you would be better off with a full if/else statement, this whole thing is totally unreadable. And the problem here, I think, is that concatenation operator has a higher precedence. So you need to do it this way: `$condition ? ($if_true) : ($if_false . $concatenated_string);`
Igor Zinov'yev
@Ibrahim Azhar Armar: To redirect to index page use `header("location: index.php");`
Sarfraz
+1  A: 
$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.

Hammerite
Hi there,in the else condidtion i want to echo something like a link to go back to index page, i tried using concatenation and it is not happening.. check the last string$user = empty($_POST['user']) ? die("Error : Please enter username") : mysql_escape_string($_POST['user']) . echo "$_SERVER['PHP_SELF']"; how do i achieve this??
Ibrahim Azhar Armar
I'm not sure what you want to achieve. If you use `$user = empty($_POST['user']) ? die("Error : Please enter username") : mysql_escape_string($_POST['user']).$_SERVER['PHP_SELF'];` then assuming the call to `empty()` returns false, `$User` will be `mysql_escape_string($_POST['user'])` concatenated with `$_SERVER['PHP_SELF']`. If you actually want to `echo` something conditionally, perhaps it would be best for you to go with an `if ... else` approach.
Hammerite
+1  A: 

It's test condition: if variable from HTML FORM is empty then print out "ERROR: Enter a country", else set variable country safe characters..

HWTech