Skip to content

Instantly share code, notes, and snippets.

@Trainmaster
Last active December 17, 2015 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Trainmaster/5575151 to your computer and use it in GitHub Desktop.
Save Trainmaster/5575151 to your computer and use it in GitHub Desktop.
Behaviour of htmlspecialchars() (or any other string function?) when passing arguments with different types
<?php
$string = 'foo';
var_dump(htmlspecialchars($string));
// string(3) "foo"
$integer = 1;
var_dump(htmlspecialchars($integer));
// string(1) "1"
$float = 1.2;
var_dump(htmlspecialchars($float));
// string(3) "1.2"
$boolean = true;
var_dump(htmlspecialchars($boolean));
// string(1) "1"
class Foo
{
public function __toString()
{
return 'Foo';
}
}
$foo = new Foo;
var_dump(htmlspecialchars($foo));
// string(3) "Foo"
$null = null;
var_dump(htmlspecialchars($null));
// string(0) ""
$object = new StdClass;
var_dump(htmlspecialchars($object));
// Warning: htmlspecialchars() expects parameter 1 to be string, object given
// NULL
$array = array();
var_dump(htmlspecialchars($array));
// Warning: htmlspecialchars() expects parameter 1 to be string, array given
// NULL
$array = (string) array();
var_dump(htmlspecialchars($array));
// string(5) "Array"
print array();
// Array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment