0

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']); 
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
  • possible duplicate of [quick php syntax question](http://stackoverflow.com/questions/889373/quick-php-syntax-question) Come on, it is nearly the same title ;) – Felix Kling Jul 30 '10 at 12:05

5 Answers5

3

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
  • 377,238
  • 77
  • 533
  • 578
  • thank you i was expecting an answer like this, i am not used to shorthand.. thank you for this reply.. :) – Ibrahim Azhar Armar Jul 30 '10 at 12:05
  • Good answer, +1, but you forgot to add `$country = ` to the `else` block – Igor Zinov'yev Jul 30 '10 at 12:05
  • @Igor Zinov'yevL Yes forgot to add that, thanks :) – Sarfraz Jul 30 '10 at 12:06
  • For more information, here's the syntax of the ternary operator : condition ? value if true : value if false – ThR37 Jul 30 '10 at 12:07
  • 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 Jul 30 '10 at 13:25
  • 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 Jul 30 '10 at 15:38
  • @Ibrahim Azhar Armar: To redirect to index page use `header("location: index.php");` – Sarfraz Jul 31 '10 at 13:24
1

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.

Community
  • 1
  • 1
Bob Fincheimer
  • 17,978
  • 1
  • 29
  • 54
1

$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
  • 1,710
  • 1
  • 15
  • 20
1
$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
  • 21,755
  • 6
  • 70
  • 91
  • 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 Jul 30 '10 at 13:27
  • 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 Jul 31 '10 at 00:31
1

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

ajile
  • 666
  • 1
  • 10
  • 18