PHP-거짓 일 때 부울을 거짓으로 반향하십시오.
다음 코드는 아무것도 인쇄하지 않습니다.
$bool_val = (bool)false;
echo $bool_val;
그러나 다음 코드가 인쇄됩니다 1.
$bool_val = (bool)true;
echo $bool_val;
인쇄 할 수있는 더 좋은 방법이 있나요 0이나 false때 $bool_val이다 false가산보다 if문장은?
echo $bool_val ? 'true' : 'false';
또는 거짓 일 때만 출력을 원할 경우 :
echo !$bool_val ? 'false' : '';
이것이 가장 쉬운 방법입니다.
$text = var_export($bool_value,true);
echo $text;
또는
var_export($bool_value)
두 번째 인수가 true가 아닌 경우 결과가 직접 출력됩니다.
아니요, 다른 옵션은 Zend 엔진을 수정하는 것이기 때문에 "더 나은 방법"이라고 부르기가 어렵습니다.
편집하다:
정말로 원한다면 배열을 사용할 수 있습니다.
$boolarray = Array(false => 'false', true => 'true');
echo $boolarray[false];
이것은 1/0 대신에 부울 값을 그대로 인쇄합니다.
$bool = false;
echo json_encode($bool); //false
나는 이것을 인쇄하기를 좋아한다.
var_dump ($var);
부울을 정수로 변환 해보십시오.
echo (int)$bool_val;
var_export 원하는 기능을 제공합니다.
이것은 또는에 대해 아무것도 인쇄하지 않고 항상 값을 인쇄합니다 . 전달 된 인수의 PHP 표현을 인쇄합니다. 출력을 복사하여 PHP에 다시 붙여 넣을 수 있습니다.nullfalsevar_export
var_export(true); // true
var_export(false); // false
var_export(1); // 1
var_export(0); // 0
var_export(null); // NULL
var_export('true'); // 'true' <-- note the quotes
var_export('false'); // 'false'
당신이 문자열을 인쇄 할 경우 "true"또는 "false", 당신은 아래와 같이 부울로 캐스팅하지만, 특색 조심 수 있습니다 :
var_export((bool) true); // true
var_export((bool) false); // false
var_export((bool) 1); // true
var_export((bool) 0); // false
var_export((bool) ''); // false
var_export((bool) 'true'); // true
var_export((bool) null); // false
// !! CAREFUL WITH CASTING !!
var_export((bool) 'false'); // true
var_export((bool) '0'); // false
이것은 제공 0또는 1:
intval($bool_val);
echo(var_export($var));
때 $var부울 변수는, true또는 false인쇄 할 수 있습니다.
삼항 연산자를 사용할 수 있습니다
echo false ? 'true' : 'false';
%b의 옵션 (sprintf와)의 정수로 부울을 변환합니다 :
echo sprintf("False will print as %b", false); //False will print as 0
echo sprintf("True will print as %b", true); //True will print as 1
If you're not familiar with it: You can give this function an arbitrary amount of parameters while the first one should be your ouput string spiced with replacement strings like %b or %s for general string replacement.
Each pattern will be replaced by the argument in order:
echo sprintf("<h1>%s</h1><p>%s<br/>%s</p>", "Neat Headline", "First Line in the paragraph", "My last words before this demo is over");
json_encode will do it out-of-the-box, but it's not pretty (indented, etc):
echo json_encode(array('whatever' => TRUE, 'somethingelse' => FALSE));
...gives...
{"whatever":true,"somethingelse":false}
function dump_condition($condition){
if($condition){
return "true";
} else {
return "false";
}
}
use on script
echo dump_condition(1>0); // print "true"
echo dump_condition(1<0); // print "false"
참고URL : https://stackoverflow.com/questions/4948663/php-get-bool-to-echo-false-when-false
'Programming' 카테고리의 다른 글
| 자동 레이아웃을 사용하는 경우 CALayer의 기준점을 어떻게 조정합니까? (0) | 2020.06.04 |
|---|---|
| Android Studio는 ProGuard 매핑 파일을 어디에 저장합니까? (0) | 2020.06.04 |
| MySQL에서 날짜 시간이 오늘보다 크거나 같음 (0) | 2020.06.04 |
| 데이터베이스의 기본 데이터 정렬을 변경하는 방법은 무엇입니까? (0) | 2020.06.04 |
| JavaScript 객체의 속성은 어떻게 계산합니까? (0) | 2020.06.03 |