3

I have a few variables namely $tv, $car, $refrigerator, $laptop and $desktop which take values 0/1 to indicate presence or absence YES/NO.

Is there an easy way to convert these from 1/0 to YES/NO?

Something which makes the below easier

<?php
 $tv = 1;
 $car = 0;
 $refrigerator = 1;
 $laptop = 1;
 $desktop = 0;

 if($tv == 1)
  $tv = "YES";
 else
  $tv = "NO";
 print($tv);


 if($car == 1)
  $car = "YES";
 else
  $car= "NO";
 print($car);

 //Repeat so on for refrigerator, laptop , desktop
?>
saltandwater
  • 791
  • 2
  • 9
  • 25

4 Answers4

3

You can write a function so you don't repeat your code over and over.

function yesNo(&$value){
    $value = $value == true ? 'Yes' : 'No';
}

yesNo($car);
yesNo($tv);
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
1

For learning propose it seems a little to complicated to me to use a function with references. So I decided to write this little example as near as possible on the given code and without references, but return values and without typecasts. 1 and 0 are integers and not boolean variables. So we will use === comparison operator to avoid type juggling:

<?php

/**
 * Convert number to 1 to'YES' or everything else to 'NO'.
 * @param $n the number to convert.
 * @return string YES|NO
 */
function numberToString($n)
{
    return $n === 1 ? 'YES' : 'NO';
}

$tv = 1;
$car = 0;
$refrigerator = 1;
$laptop = 1;
$desktop = 0;

// Call numberToString function for $tv and print return value
print(numberToString($tv));

// Call numberToString function for $car and print return value
print(numberToString($car));

// Call numberToString function for $refrigerator and print return value
print(numberToString($refrigerator));

// Call numberToString function for $laptop and print return value
print(numberToString($laptop));

// Call numberToString function for $desktop and print return value
print(numberToString($desktop));

?>

And if you feel lucky you can also use array with foreach:

<?php

/**
 * Convert number to 1 to'YES' or everything else to 'NO'.
 * @param $n the number to convert.
 * @return string YES|NO
 */
function numberToString($n)
{
    return $n === 1 ? 'YES' : 'NO';
}

// For php >= 5.4 you can also use the new array syntax:
// $devices = [
//     'tv' => 1,
//     'car' => 0,
//     'refrigerator' => 1,
//     'laptop' => 1,
//     'desktop' => 0,
// ];

$devices = array(
    'tv' => 1,
    'car' => 0,
    'refrigerator' => 1,
    'laptop' => 1,
    'desktop' => 0,
);

foreach ($devices as $device) {
    // Call numberToString function for $tv and print return value
    print(numberToString($device));
}

?>

Happy coding.

skroczek
  • 2,289
  • 2
  • 16
  • 23
1

skroczek's method worked for me in an array, but I had to update to the following for it to work:

function numberToString($n)
{
    return $n == 1 ? 'YES' : 'NO';
}
d4np3
  • 41
  • 2
  • 1
    Loose `==` and strict `===` operators are a bit different. More about it here: http://stackoverflow.com/questions/80646/how-do-the-php-equality-double-equals-and-identity-triple-equals-comp – Silver Ringvee May 02 '16 at 13:23
1

for those who see this post recently
filter_var function does this with ease

var_dump(filter_var('oops', FILTER_VALIDATE_BOOLEAN)); // bool(false)
var_dump(filter_var('no', FILTER_VALIDATE_BOOLEAN)); // bool(false)
var_dump(filter_var('yes', FILTER_VALIDATE_BOOLEAN)); // bool(true)
var_dump(filter_var('1', FILTER_VALIDATE_BOOLEAN)); //bool(true)
var_dump(filter_var('0', FILTER_VALIDATE_BOOLEAN)); //bool(false)
var_dump(filter_var('2', FILTER_VALIDATE_BOOLEAN)); //bool(false)
a.sadegh63
  • 56
  • 1
  • 8