Asked  7 Months ago    Answers:  5   Viewed   33 times

Do you know why <?= count(false) ?> returns 1?



It's specified behavior:

If var is not an array or an object with implemented Countable interface, 1 will be returned.

According to

Wednesday, March 31, 2021
answered 7 Months ago

If you are using PHP >= 5.5, you can use array_column(), in conjunction with array_count_values():

$colors = array_count_values(array_column($log, 0));
$materials = array_count_values(array_column($log, 1));

See demo

Or, if you're not using PHP >= 5.5, this will work in PHP 4, 5:

$colors = $materials = array();
foreach ($log as $a){
    $colors[] = $a[0];
    $materials[] = $a[1];

$colors = array_count_values($colors);
$materials = array_count_values($materials);

See demo 2

Click here for sample use case that will work with either method.

Wednesday, March 31, 2021
answered 7 Months ago

Assuming by 'area' and 'source' you mean arbitrary strings, you could nest a few loops like so:

$num_titles = 0;
foreach ($doclist as $area => $arr1) {
    foreach ($arr1 as $source => $arr2) {
        foreach ($arr2 as $k => $arr3) {
            if (isset($arr3['title']) && strlen(trim($arr3['title'])))
print "Titles: {$num_titles}n";
print "Areas: " . sizeof($doclist) . "n";
Wednesday, March 31, 2021
answered 7 Months ago

The interactive interpreter will print whatever is returned by the expression you type and execute, as a way to make testing and debugging convenient.

>>> 5
>>> 42
>>> 'hello'
>>> (lambda : 'hello')()
>>> def f():
...     print 'this is printed'
...     return 'this is returned, and printed by the interpreter'
>>> f()
this is printed
'this is returned, and printed by the interpreter'
>>> None

See Read–eval–print loop on Wikipedia for more information about this.

Saturday, July 31, 2021
answered 3 Months ago

I would say that requiring this to make the program ill-formed (that is, make this a compilation error) would complicate the standard considerably for little benefit. You'd have to exactly spell out in the standard when such cases shall be diagnosed, and all compilers would have to implement them.

If you specify too little, it will not be too useful. And compilers probably already check for this to emit warnings, and real programmers compile with -Wall_you_can_give_me -Werror anyway.

If you specify too much, it will be difficult (or impossible) for compilers to implement the standard.

Consider this class (for which you only have the header and a library):

class Foo
  int x;

  int& getInteger();

And this code:

int& bar()
  Foo f;
  return f.getInteger();

Now, should the standard be written to make this ill-formed or not? Probably not, what if Foo is implemented like this:

#include "Foo.h"

int global;

int& Foo::getInteger()
  return global;

At the same time, it could be implemented like this:

#include "Foo.h"

int& Foo::getInteger()
  return x;

Which of course would give you a dangling reference.

My point is that the compiler cannot really know whether returning a reference is OK or not, except for a few trivial cases (returning a reference to a function-scope automatic variable or parameter of non-reference type). I don't think it's worth it to complicate the standard for that. Especially as most compilers already warn about this as a quality-of-implementation matter.

Tuesday, August 3, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :