Print false in php

I am trying to print the following statement:

print false . "\n" . true . "\n"; echo false . (bool)false . "\n" . true . "\n"; print "0" . "\n" . true . "\n"; 

As a result, I get only "1 1 0 1". Expected Result:

0
1
0
1
0
1

I am using PHP 5.4.3 MSVC9 x64 Can someone explain why and how I can print the correct path?

+2
source share
2 answers

That should do the trick. Use an array.

 $boolarray = Array(false => 'false', true => 'true'); echo $boolarray[false]," ", $boolarray[true]; 

Output: false true

+2
source

Your problem arises due to your error of the + operator in strings in PHP. String concatenation operator . since PHP is free to type, it doesn’t know if you want to concatenate or add strings.

I will break you:

 print false + "\n" + true + "\n"; echo false+(bool)false + "\n" + true + "\n"; print "0" + "\n" + true + "\n"; 

First you can choose to stay with echo or print . Now, go ahead:

 print false + "\n" + true + "\n"; 

PHP strings, when added (without contact) , evaluate to 0 . Therefore, this operator evaluates:

 print 0 + 0 + 1 + 0; 

which is equal to 1 . The rest follow this example. If you want your code to work, you must use the concatenation operator ( . ). If you want to write True or False , as .NET does, you can write a simple function:

 function writeBool($var) { echo ($var) ? "True" : "False"; } 

According to the fact that PHP is typing (which sucks if you ask me), everything that evaluates to True will write "True". I still won’t use a function to do this, since function calls in PHP are expensive .

+10
source

Source: https://habr.com/ru/post/921274/


All Articles