I was testing Heredoc and || functionality
function test()
{
$obj = [(object)['col1'=>'val1'],
(object)['col2'=>'val2'],
(object)['col3'=>'val3']];
$output = '';
$mystring = <<<EOT
a non empty string
EOT;
foreach($obj as $prop)
{
foreach($prop as $i)
{
$output .= <<<EOT
<div style='background-color:lightblue'>
\$head : {gettype($i)}
</div>
EOT;
}
}
$out = $output || $mystring || 'couldn\'t find something valuable';
echo $out;
}
test();
I get the output of
1
which represents a Boolean true. I had it output the contents of $mystring at one point by putting the logic in brackets eg
echo ($output || $mystring);
and it used to output:
a non empty string
It stopped working straight after and I am not sure what change broke it.
In javascript:
function test()
{
var obj = [{'col1':'val1'},
{'col2':'val2'},
{'col3':'val3'}];
var output = '';
var mystring ="a non empty string";
for(var prop in obj)
{
for(var i in prop)
{
output +=
"<div style='background-color:lightblue'>" +
i + " : " + typeof prop[i] +
"</div>";
}
}
out = output || mystring || 'couldn\'t find something valuable';
document.write(out);
console.log("outputting \n\t" + out);
}
test();
It is slightly different from the php in terms of logic but the || during assignment works as expected. I get
0 : string
0 : string
0 : string
for the code above. If comment out the inner for loop like so
for(var prop in obj)
{
/*for(var i in prop)
{
output +=
"<div style='background-color:lightblue'>" +
i + " : " + typeof prop[i] +
"</div>";
}*/
}
I get the contents of "mystring" which is
a non empty string
and if I then change mystring to an empty string like so
mystring = ""; //"a non empty string";
I get
couldn't find something valuable
How does php's "||" during assignment work ? Can someone explain it?
function test() {and} test()is unformatted.