PHP is there a way to make an if statement a variable?

Viewed 38

I am trying to make an if statement in to a variable. Is it possible.

I want to make the output of this code:

<?php f($_GET['c_p_id'] == '3932'){ echo '6089'; }else{  echo '2939'; }?>

in to a $price variable. How can I do so?

2 Answers

Do you mean this?

<?php
if ($_GET['c_p_id'] == '3932') {
  $price = '6089';
} else {
  $price = '2939';
}
?>

Are referring to this? This is the only way I know of writing an if statement as a variable.

# Format:
$variable = (parameters) ? 'parameter is true' : 'parameter is false';

# Usage:
$price = ($_GET['c_p_id'] == '3932') ? 6089 : 2939;
echo($price);

That would be identical to this:

if($_GET['c_p_id'] == '3932'){
    $price = 6089;
}else{
    $price = 2939;
}
echo($price);
Related