Jump to content

[SOLVED] Convert the value of bool to string without conditional?


DssTrainer

Recommended Posts

This is a silly one. But I am setting a variable to a value from the database

 

$var = "1";

 

If $var is not 0, i want to convert it to the string "true"

if $var is 0, I want the string "false"

 

I tried (string)(bool)$var;

 

but they are conflicting designs, since (string) changes boolean to "1". I want it to convert to the actual text "true".

 

I could easily do it with a simple if statement. But I was wondering if there is a way to use the php functions to do it.

I'm not sure, but what I understand from the question is this. is this what you mean?

 

<?php
if ($var == 0){
$var = false;
}
elseif ($var != 0){
$var = true;
}

?>

 

Yea this is my end result that I want. But I wondered if there was a way to do it with type conversion.

I want to convert 1 to true.. and then convert true to "true" (as a string).

Yes, that's the correct syntax ProjectFear.

 

DssTrainer, members have already helped you with PHP answers, and here's how to do it in MySQL if you want:

 

select

        if(column_name = 1, 'true', 'false') as boolean_string_var

from

        table_name

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.