in_array really loves typecast

<?php
$a
= array('7.1');
var_dump(in_array('7.10', $a));
?>

bool(true)
I have no explanation. Both are strings. Really: WTF?

»

More on array references

<?php
function test(&$a) {}
test($x['foo']);
?>

Don't ask me, ever, why this doesn't throw a notice or two. It creates the $x array with a foo key assigned to NULL.

»

PHP protected and an assault

<?php
class Victim {
  protected
$money = 100;

  public function
__toString() {
    return (string)
$this->money;
  }
}

class
Offender extends Victim {
  function
assault(Victim $victim) {
   
$victim->money = 0;
  }
}

$victim = new Victim;
$offender = new Offender;
$offender->assault($victim);
print (string)
$victim;
?>

»

PHP function calls returning references

Let's say we have this function:

<?php
function &collector() {
  static
$c = array();
  return
$c;
}
?>

we can use this like the following (very familiar to D7 coders):
<?php
 
// Note that $x = collector(); won't work.
 
$x = &collector();
 
$x[] = 'foo';
?>

Say you wanted to have one line code:
<?php
  array_push
(&collector(), 'foo');
?>

»

PHP function calls have quite some overhead

Apparently $array === (array) $array is significantly faster than is_array($array) for very small arrays. I have posted a detailed benchmark script which seems to concur: for approximately 40-50 bytes, like an array containing 5-6 integers (I am on 64 bit) or 5-6 of 8 byte strings this tricky is definitely faster.

»

foreach and references

Apparently this is not a bug and it's documented:

<?php
$array
= array('foo', 'bar');
foreach (
$array as &$foo);
foreach (
$array as $foo);
var_dump($array);
?>

»

Array reference always succeeds

php -r 'var_dump($a["foo"]);var_dump(&$a["foo"]);var_dump($a);'

»

Static properties and variable classnames

Oh no!

<?php
class test {
  static public
$foo = 'test1';
  static public
$bar = 'test2';
  function
foo() {
    print
"method foo\n";
  }
}

class
test1 {
  function
__construct() {
    print
"test1 constructor\n";
  }
}
class
test2 {
  function
__construct() {
    print
"test2 constructor\n";
  }
}

function
test1() {
  print
"test1 function\n";
}
function
test2() {
  print
"test2 function\n";
}
function
foo() {
  print
"foo function\n";
  return
'bar';
}

$foo = 'foo';

$a = test::$foo;
new
$a();
new
test::$foo();
$a();
print
test::$foo();
?>

»

On class properties

<?php
class foo  {
  static public
$bar = 2;
}

print
foo::$bar;
$n = new foo;
print
$n->bar;
print
$n->$bar;
?>

Results?

»

_FILES array

An <input type="file" name="foo"> produces this

<?php
array (
 
'foo' =>
  array (
   
'name' => '',
   
'type' => '',
   
'tmp_name' => '',
   
'error' => 4,
   
'size' => 0,
  ),
)
?>

$_FILES array. What do you think foo[bar] will produce?

»
Syndicate content