Viewed   113 times

How can I convert string to boolean?

$string = 'false';

$test_mode_mail = settype($string, 'boolean');

var_dump($test_mode_mail);

if($test_mode_mail) echo 'test mode is on.';

it returns,

boolean true

but it should be boolean false.

 Answers

4

Strings always evaluate to boolean true unless they have a value that's considered "empty" by PHP (taken from the documentation for empty):

  1. "" (an empty string);
  2. "0" (0 as a string)

If you need to set a boolean based on the text value of a string, then you'll need to check for the presence or otherwise of that value.

$test_mode_mail = $string === 'true'? true: false;

EDIT: the above code is intended for clarity of understanding. In actual use the following code may be more appropriate:

$test_mode_mail = ($string === 'true');

or maybe use of the filter_var function may cover more boolean values:

filter_var($string, FILTER_VALIDATE_BOOLEAN);

filter_var covers a whole range of values, including the truthy values "true", "1", "yes" and "on". See here for more details.

Tuesday, October 18, 2022
1
$rootbeer = (float) $InvoicedUnits;

Should do it for you. Check out Type-Juggling. You should also read String conversion to Numbers.

Monday, August 22, 2022
1

The exact rules are spelled out in the Java Language Specification, §5.1.11. String Conversion

According to those rules, "str" + bool is equivalent to:

"str" + new Boolean(bool).toString()

That said, the compiler is permitted considerable leeway in how exactly the overall expression is evaluated. From JLS §15.18.1. String Concatenation Operator +:

An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

For primitive types, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.

For example, with my compiler the following:

boolean bool = true;
System.out.println("the value of bool is : " + bool);

is exactly equivalent to:

boolean bool = true;
System.out.println(new StringBuilder("the value of bool is : ").append(bool).toString());

They result in identical bytecodes:

Code:
   0: iconst_1      
   1: istore_1      
   2: getstatic     #59                 // Field java/lang/System.out:Ljava/io/PrintStream;
   5: new           #166                // class java/lang/StringBuilder
   8: dup           
   9: ldc           #168                // String the value of bool is : 
  11: invokespecial #170                // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
  14: iload_1       
  15: invokevirtual #172                // Method java/lang/StringBuilder.append:(Z)Ljava/lang/StringBuilder;
  18: invokevirtual #176                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
  21: invokevirtual #69                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
  24: return        
Sunday, December 4, 2022
 
5

Method 1 :

var stringValue = "true";
var boolValue = (/true/i).test(stringValue) //returns true

Method 2 :

var stringValue = "true";
var boolValue = (stringValue =="true");   //returns true

Method 3 :

var stringValue = "true";
var boolValue = JSON.parse(stringValue);   //returns true

Method 4 :

var stringValue = "true";
var boolValue = stringValue.toLowerCase() == 'true'; //returns true

Method 5 :

var stringValue = "true";
var boolValue = getBoolean(stringValue); //returns true
function getBoolean(value){
   switch(value){
        case true:
        case "true":
        case 1:
        case "1":
        case "on":
        case "yes":
            return true;
        default: 
            return false;
    }
}

source: http://codippa.com/how-to-convert-string-to-boolean-javascript/

Monday, August 22, 2022
 
2

Returning code from server is terrible awful. Anyways, this code I give you do some steps:

  1. removes new lines and multispaces into one length space from input.
  2. finds words by pattern where word starts with if and ends with digits. Adds them to array.
  3. prepares pattern with user chosen options combining into similar to (Q4a.NAOK=="7" && Q4b.NAOK=="5") (answer 1 and answer2).
  4. checking if array contains word like in point 3. If found, retains word from array and split by comma. Second word from splitting is the answer.

private void doStuff() {
    int answer1 = 2;
    int answer2 = 1;

    String s = "{hidSection=if(((Q4a.NAOK=="5" && Q4b.NAOK=="7")|| (Q4a.NAOK=="6" && Q4b.NAOK=="6") || (Q4a.NAOK=="6" && Q4b.NAOK=="7")|| n" +
            "            (Q4a.NAOK=="7" && Q4b.NAOK=="5")|| (Q4a.NAOK=="7" && Q4b.NAOK=="6") || (Q4a.NAOK=="7" && Q4b.NAOK=="7")|| n" +
            "            (Q4a.NAOK=="8" && Q4b.NAOK=="7")|| (Q4a.NAOK=="12" && Q4b.NAOK=="6")|| (Q4a.NAOK=="12" && Q4b.NAOK=="7"))n" +
            "    ,1,if(((Q4a.NAOK=="4" && Q4b.NAOK=="6") || (Q4a.NAOK=="4" && Q4b.NAOK=="7") || (Q4a.NAOK=="5" && Q4b.NAOK=="5")||n" +
            "            (Q4a.NAOK=="5" && Q4b.NAOK=="6")|| (Q4a.NAOK=="7" && Q4b.NAOK=="3") || (Q4a.NAOK=="6" && Q4b.NAOK=="5")|| n" +
            "            (Q4a.NAOK=="7" && Q4b.NAOK=="4")|| (Q4a.NAOK=="8" && Q4b.NAOK=="6")|| (Q4a.NAOK=="10" && Q4b.NAOK=="7")|| n" +
            "            (Q4a.NAOK=="11" && Q4b.NAOK=="6")|| (Q4a.NAOK=="11" && Q4b.NAOK=="7")|| (Q4a.NAOK=="12" && Q4b.NAOK=="5"))n" +
            "    ,2,if(((Q4a.NAOK=="4" && Q4b.NAOK=="5") || (Q4a.NAOK=="5" && Q4b.NAOK=="4")|| (Q4a.NAOK=="6" && Q4b.NAOK=="4")|| n" +
            "            (Q4a.NAOK=="7" && Q4b.NAOK=="1")|| (Q4a.NAOK=="7" && Q4b.NAOK=="2")|| (Q4a.NAOK=="8" && Q4b.NAOK=="5")|| n" +
            "            (Q4a.NAOK=="9" && Q4b.NAOK=="6")|| (Q4a.NAOK=="9" && Q4b.NAOK=="7")|| (Q4a.NAOK=="10" && Q4b.NAOK=="6")|| n" +
            "            (Q4a.NAOK=="11" && Q4b.NAOK=="5")|| (Q4a.NAOK=="12" && Q4b.NAOK=="1")|| (Q4a.NAOK=="12" && Q4b.NAOK=="2")|| n" +
            "            (Q4a.NAOK=="12" && Q4b.NAOK=="3")|| (Q4a.NAOK=="12" && Q4b.NAOK=="4"))n" +
            "    ,3,if(((Q4a.NAOK=="2" && Q4b.NAOK=="6") || (Q4a.NAOK=="2" && Q4b.NAOK=="7")|| (Q4a.NAOK=="3" && Q4b.NAOK=="6")|| n" +
            "            (Q4a.NAOK=="3" && Q4b.NAOK=="7")|| (Q4a.NAOK=="4" && Q4b.NAOK=="4")|| (Q4a.NAOK=="5" && Q4b.NAOK=="3")|| n" +
            "            (Q4a.NAOK=="6" && Q4b.NAOK=="2")|| (Q4a.NAOK=="6" && Q4b.NAOK=="3")|| (Q4a.NAOK=="8" && Q4b.NAOK=="4")|| n" +
            "            (Q4a.NAOK=="9" && Q4b.NAOK=="5")|| (Q4a.NAOK=="10" && Q4b.NAOK=="5")|| (Q4a.NAOK=="11" && Q4b.NAOK=="4"))n" +
            "    ,4,if(((Q4a.NAOK=="2" && Q4b.NAOK=="4") || (Q4a.NAOK=="2" && Q4b.NAOK=="5")|| (Q4a.NAOK=="3" && Q4b.NAOK=="4")||                (Q4a.NAOK=="3" && Q4b.NAOK=="5")|| n" +
            "            (Q4a.NAOK=="4" && Q4b.NAOK=="3")|| (Q4a.NAOK=="5" && Q4b.NAOK=="2")|| (Q4a.NAOK=="6" && Q4b.NAOK=="1")|| n" +
            "            (Q4a.NAOK=="9" && Q4b.NAOK=="4")|| (Q4a.NAOK=="10" && Q4b.NAOK=="3")|| (Q4a.NAOK=="10" && Q4b.NAOK=="4")|| n" +
            "            (Q4a.NAOK=="11" && Q4b.NAOK=="1")|| (Q4a.NAOK=="11" && Q4b.NAOK=="2")|| (Q4a.NAOK=="11" && Q4b.NAOK=="3"))n" +
            "    ,5,if(((Q4a.NAOK=="1" && Q4b.NAOK=="4") || (Q4a.NAOK=="1" && Q4b.NAOK=="5")|| (Q4a.NAOK=="1" && Q4b.NAOK=="6")|| n" +
            "            (Q4a.NAOK=="1" && Q4b.NAOK=="7")|| (Q4a.NAOK=="2" && Q4b.NAOK=="3")|| (Q4a.NAOK=="3" && Q4b.NAOK=="2")|| n" +
            "            (Q4a.NAOK=="3" && Q4b.NAOK=="3")|| (Q4a.NAOK=="4" && Q4b.NAOK=="1")|| (Q4a.NAOK=="4" && Q4b.NAOK=="2")|| n" +
            "            (Q4a.NAOK=="5" && Q4b.NAOK=="1")|| (Q4a.NAOK=="8" && Q4b.NAOK=="1")|| (Q4a.NAOK=="8" && Q4b.NAOK=="2")|| n" +
            "            (Q4a.NAOK=="8" && Q4b.NAOK=="3")|| (Q4a.NAOK=="9" && Q4b.NAOK=="1")|| (Q4a.NAOK=="9" && Q4b.NAOK=="2")||                n" +
            "            (Q4a.NAOK=="9" && Q4b.NAOK=="3")|| (Q4a.NAOK=="10" && Q4b.NAOK=="1")|| (Q4a.NAOK=="10" && Q4b.NAOK=="2"))n" +
            "    ,6,if(((Q4a.NAOK=="1" && Q4b.NAOK=="3") || (Q4a.NAOK=="2" && Q4b.NAOK=="2"))n" +
            "    ,7,if(((Q4a.NAOK=="1" && Q4b.NAOK=="1") || (Q4a.NAOK=="1" && Q4b.NAOK=="2") || (Q4a.NAOK=="2" && Q4b.NAOK=="1") || n" +
            "            (Q4a.NAOK=="3" && Q4b.NAOK=="1"))n" +
            "    ,8,""))))))))}";

    String answer = getAnswer(answer1, answer2, s);
    Log.d("xxx", "answer:" + answer);
}

private String getAnswer(int answer1, int answer2, String s) {
    //remove spaces 2 or more length and replace to 1
    s = s.replaceAll("n", "");
    s = s.replaceAll("\s+", " ");

    ArrayList<String> states = new ArrayList<String>();

    Pattern p = Pattern.compile("if\({3}.*?\,\d+");
    Matcher m = p.matcher(s);
    while (m.find()) {
        states.add(m.group());
    }

    String stateFormat = "Q4a.NAOK=="%d" && Q4b.NAOK=="%d"";
    String stateFormatted = String.format(stateFormat, answer1, answer2);

    for (int i = 0; i < states.size(); i++) {
        if (states.get(i).contains(stateFormatted)) {
            String[] items = states.get(i).split(",");
            if (items.length > 1) {
                return items[1].trim();
            }
        }
    }

    return null;//or null or integer -1
}
Thursday, October 27, 2022
 
tavier
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :