20 days later

TIL that returning false works if you declare return type int:

function foo() : int {
        return false;
}
var_dump(foo());

the result:

int(0)

but returning NULL from an int function throws an exception:

function foo() : int {
        return null;
}
var_dump(foo());

the result:

PHP Fatal error:  Uncaught TypeError: Return value of foo() must be of the type integer, null returned in /tmp/a.php:3
Stack trace:
#0 /tmp/a.php(5): foo()
#1 {main}
  thrown in /tmp/a.php on line 3

sneakyimp

You'll also get that if you try to return a non-numeric string (a numeric string would be recast).

Also, attempting to return anything other than an int fail if you declare(strict_types=1);.

Returning null is hazardous, and equivalent to returning nothing at all (return null; is identical in meaning to return;; if a function is supposed to return a value, return; looks wrong). If for some reason you do want nulls hanging around, you can declare the possibility as function foo(): ?int.

14 days later

Today I learned Mira Furlan is dead, this year sucks let's go to 2022

    a month later

    TIL that the U.S. Government website for reporting online crime does not work:

     curl -v https://www.ic3.gov/complaint/default.aspx
    *   Trying 65.201.175.169:443...
    * TCP_NODELAY set
    *   Trying 2600:803:c20::3:16:443...
    * TCP_NODELAY set
    * Immediate connect fail for 2600:803:c20::3:16: Network is unreachable
    *   Trying 2600:803:c20::3:15:443...
    * TCP_NODELAY set
    * Immediate connect fail for 2600:803:c20::3:15: Network is unreachable
    *   Trying 2600:803:c20::3:16:443...
    * TCP_NODELAY set
    * Immediate connect fail for 2600:803:c20::3:16: Network is unreachable
    *   Trying 2600:803:c20::3:15:443...
    * TCP_NODELAY set
    * Immediate connect fail for 2600:803:c20::3:15: Network is unreachable

      Today I learned PHP filters. I really enjoy the PHP language while I am learning it.

        TIL (thanks to sneakyimp) that you can do...

        16:16 $ /usr/bin/php -a
        Interactive shell
        
        php > $😊 = 'happy';
        php > echo $😊;
        happy
        php >
        

        you can define emoji classes and namespaces, too:

        class 😊 {
          public static function â˜šī¸(){
            echo "🎭\n";
          }
        }
        😊::â˜šī¸();
        6 days later
        4 days later

        Today I learned how to turn my old laptop into a server at home

        sneakyimp Running it on windows machine

        Today I learned to set up virtual machines and pint to different projects

          3 months later

          Okay, so array_map(null, $array1, $array2, $array, ...) can be used to zip several arrays together. array_map(null, [1,2,3], [4,5,6]) == [[1,4], [2,5], [3,6]]. And then you've got the destructuring operator "..." and now you can have a general array-transpose operation:

          $array = [[1,2,3], [4,5,6], [7,8,9]];
          array_map(null, ...$array) == [[1,4,7], [2,5,8], [3,6,9]];
          

          Which is all well, and good. But TIL:

          array_map(null, ...[[a,b,c], [d,e,f], [g,h,i], [j,k,l]]) == [[a,d,g,j], [b,e,h,k], [c,f,i,l]]
          array_map(null, ...[[a,b,c], [d,e,f], [g,h,i]]) == [[a,d,g], [b,e,h], [c,f,i]]
          array_map(null, ...[[a,b,c], [d,e,f]]) == [[a,d], [b,e], [c,f]]
          array_map(null, ...[[a,b,c]]) == [a, b, c]
          

          4 arrays of 3 elements each becomes 3 arrays of 4 elements each
          3 arrays of 3 elements each becomes 3 arrays of 3 elements each
          2 arrays of 3 elements each becomes 3 arrays of 2 elements each
          1 array  of 3 elements each becomes 1 array  of 3 elements each

          One of these things is not like the others. And apparently it's not a bug. array_map(null, ...[[a,b,c]]) is equivalent to array_map(null, [a,b,c]) as it should be, but it was decided that the latter should be a no-op and just return its second argument unchanged.

          Weedpacket

          if(!is_array($new_array[0])) {
              $new_array = [$new_array];
          }
          

          🙃

          (It's been years since I touched array_map() -- thankfully? -- and have never run into that syntax, and kind of hope I never have a reason to use it. 😉 )

          Weedpacket You don't like functional programming?

          Not a question of like/dislike, but more that I've not pursued it, other than reading/viewing a few high-level discussions at mainly theoretical levels -- no practical experience (intentionally) implementing it.

          NogDog Ah; your mention of "hope" made me think you had something against it.

          For fun and the record, here's the method where I hit the snag, with the workaround included, just to give an idea of what things would start to look like without the array_map.

          public function ConjunctionVisit(Conjunction $expr)
          {
          	// Look for substitutions to Conjunction-bound variables. If this happens (it shouldn't, but at present
          	// there's nothing to stop it), we first replace the Conjunction's colliding bound variables with
          	// newly-minted ones, then update its expression to use the newly-minted variables in place of
          	// the existing ones. Then we can apply the substitutions we were actually passed.
          	$new_substitutions = [];
          	foreach($this->substitutions as [$var,])
          	{
          		if(in_array($var, $expr->variables, true))
          		{
          			$new_substitutions[] = [$var, LVariable()];
          		}
          	}
          	if(empty($new_substitutions))
          	{
          		// No substitutions of bound variables. Business as usual, then.
          		$expression = $expr->expression->accept($this);
          		return ($expression === $expr->expression) ? $expr : Conjunction($expression, $expr->variables);
          	}
          
          // If the number of new substitutions is 1 then array_map(null, ...) breaks down
          // (this is known but undocumented behaviour: array_map(null, array) is a no-op)
          if(count($new_substitutions) == 1)
          {
          	$existing_variables = [$new_substitutions[0][0]];
          	$new_variables = [$new_substitutions[0][1]];
          }
          else
          {
          	[$existing_variables, $new_variables] = array_map(null, ...$new_substitutions);
          }
          $new_variables = array_map(function($var)use($existing_variables, $new_variables)
          {
          	$i = array_search($var, $existing_variables, true);
          	return ($i !== false) ? $new_variables[$i] : $var;
          }, $expr->variables);
          return Conjunction($expr->expression->accept(Substituting(...$new_substitutions, ...$this->substitutions)), $new_variables);
          }
          

          Weedpacket your mention of "hope" made me think you had something against it

          Nah, more that I had recollections of trying to use array_map() for something way back when and finding it less than easy to comprehend at the time, and that this twist on it looked like it might cause a similar mind-ache. 🙂 (Hopefully I'm a bit better programmer now and maybe it would come more easily, but the situation has not arisen since then where I ended up on the manual page for array_map().)

            3 months later

            TIL: you can flip an image horizontally just via CSS:

            <img src='/smurf.gif' height="150px" style="transform: scaleX(-1);">
            

            PS: Yes, I'm using a "Smurf" image for an internal tool on a project which ended up with the acronym "SMRF". 🙂