DssTrainer Posted August 29, 2008 Share Posted August 29, 2008 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. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted August 29, 2008 Share Posted August 29, 2008 not quite sure what you mean sorry? This would be much simpler using a if, or the ternary operator(if you want a shorter code) Quote Link to comment Share on other sites More sharing options...
kratsg Posted August 29, 2008 Share Posted August 29, 2008 Well... if you do: $var = "1"; $var = $var*1; That converts $var from a string to an integer/number. It should be straightforward for a boolean. Quote Link to comment Share on other sites More sharing options...
Vermillion Posted August 29, 2008 Share Posted August 29, 2008 You want to do something similar to a C/C++ Cast type? Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted August 29, 2008 Share Posted August 29, 2008 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; } ?> Quote Link to comment Share on other sites More sharing options...
DssTrainer Posted August 29, 2008 Author Share Posted August 29, 2008 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). Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 29, 2008 Share Posted August 29, 2008 What would you need that for anyway? You would just add code, and i'm sure that checking against a bool variable is faster then against a string. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 30, 2008 Share Posted August 30, 2008 I know you said you don't want an if statement, but I'd go with the ternary operator: $var=($var):'true':'false'; Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 30, 2008 Share Posted August 30, 2008 @mjdamato: Isn't the syntax this. $var = ($var) ? 'true' : 'false'; Quote Link to comment Share on other sites More sharing options...
toplay Posted August 30, 2008 Share Posted August 30, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.