How to Convert String to Boolean PHP

How to convert string to boolean php

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.

Parsing a string into a boolean value in PHP

There is a native PHP method of doing this which uses PHP's filter_var method:

$bool = filter_var($value, FILTER_VALIDATE_BOOLEAN);

According to PHP's manual:

Returns TRUE for "1", "true", "on" and "yes". Returns FALSE otherwise.

If FILTER_NULL_ON_FAILURE is set, FALSE is returned only for "0", "false", "off", "no", and "", and NULL is returned for all non-boolean values.

Convert String 'true' or 'false' to interger '1' or '0'

Strings always evaluate to boolean true unless they have a value that's considered "empty" by PHP.

Depending on your needs, you should consider using filter_var() with the FILTER_VALIDATE_BOOLEAN flag.

(int)filter_var('true', FILTER_VALIDATE_BOOLEAN);
(int)filter_var('false', FILTER_VALIDATE_BOOLEAN);

Whats the laravel way to convert boolean string to bool?

    if(!filter_var($render, FILTER_VALIDATE_BOOLEAN)) {

// is falsy

}

Encapsulate this in a string macro. In the AppServiceProvider boot method add;

use Illuminate\Support\Str;

public function boot()
{
Str::macro('isFalsy', function ($str) {
return !filter_var($str, FILTER_VALIDATE_BOOLEAN);
});
}

now in code Str::isFalsy($render) or in Laravel 9 str($render)->isFalsy()

or swap the logic and create an isTruthy() macro

How to Convert Boolean to String

Simplest solution:

$converted_res = $res ? 'true' : 'false';

How to convert numbers into boolean in Julia?

The quick answer is that it seems like you want to test whether a value is non-zero or not and you can test if a number x is non-zero like this: x != 0. Whether a number is considered true or false depends on what language you're in, however; more on that below.

Having gotten the quick answer out of the way, we can talk about why. The docs say that false and true are numerically equal to zero and one. We can test this out ourselves:

julia> false == 0
true

julia> true == 1.0
true

You can see that the equality holds regardless of numerical type—integers or floats or wven more esoteric number types like complexes or rationals. We can also convert booleans to other types of numbers:

julia> Int(false)
0

julia> Float64(true)
1.0

We can convert those values back to booleans:

julia> Bool(0)
false

julia> Bool(1.0)
true

What you can't do is convert a number that isn't equal to zero or one to boolean:

julia> Bool(5)
ERROR: InexactError: Bool(5)

That is because the Bool type can only represent the values zero and one exactly and trying to convert any other numeric value to Bool will give an InexactError. This is the same error you get if you try to convert a float that isn't integer-valued to an integer type:

julia> Int(5.0)
5

julia> Int(5.5)
ERROR: InexactError: Int64(5.5)

Or an integer to a smaller type that isn't big enough to represent that value:

julia> Int8(123)
123

julia> Int8(1234)
ERROR: InexactError: trunc(Int8, 1234)

The exact same thing is happening here: Bool is not big enough to represent the value 5, so you get an error if you try to convert the value 5 to Bool.

The convention that many languages use for truthiness of numbers is that values that represent zero are falsey and values that represent no-zeros are truthy. Note that there is no sound mathematical reason for this: zero is not false and non-zero numbers are not true; it's just a convention that comes from the C programming language, which doesn't have a boolean type and uses this convention for treating integers as true/false in conditionals. This convention is far from universal, however, as there are popular languages that don't follow it: in Lisps and Ruby, for example, all numbers are truthy. I wrote a post recently on the Julia discourse forum about differing notions of truthiness in different languages and why Julia rejects truthiness and instead requires you to write out an explicit condition like comparison with zero for numbers, or non-emptiness for collections.

Since the condition you actually want to check is whether a number is equal to zero or not, that's just what you should do: explicitly compare the number to zero by doing x != 0. This will evaluate to a boolean value: true if x is non-zero and false if x is equal to zero. There's a predicate function that does this as well: iszero(x) checks if x is zero, which can be handy if you want to e.g. count how many zero or non-zero values there are in a collection:

julia> v = rand(-2:2, 100);

julia> count(iszero, v)
18

julia> count(!iszero, v)
82

Convert String 'true' or 'false' to interger '1' or '0'

Strings always evaluate to boolean true unless they have a value that's considered "empty" by PHP.

Depending on your needs, you should consider using filter_var() with the FILTER_VALIDATE_BOOLEAN flag.

(int)filter_var('true', FILTER_VALIDATE_BOOLEAN);
(int)filter_var('false', FILTER_VALIDATE_BOOLEAN);


Related Topics



Leave a reply



Submit