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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.