PageRenderTime 200ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/bad/ext/standard/tests/general_functions/gettype_settype_basic.php

http://github.com/facebook/hiphop-php
PHP | 103 lines | 70 code | 16 blank | 17 comment | 0 complexity | 484ec5df1836e1844e110d7250826960 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. /* Prototype: string gettype ( mixed $var );
  3. Description: Returns the type of the PHP variable var
  4. Prototype: bool settype ( mixed &$var, string $type );
  5. Description: Set the type of variable var to type
  6. */
  7. /* Test the basic functionalities of settype() & gettype() functions.
  8. Use the gettype() to get the type of regular data and use settype()
  9. to change its type to other types */
  10. /* function to handle catchable errors */
  11. function foo($errno, $errstr, $errfile, $errline) {
  12. // var_dump($errstr);
  13. // print error no and error string
  14. echo "$errno: $errstr\n";
  15. }
  16. //set the error handler, this is required as
  17. // settype() would fail with catachable fatal error
  18. set_error_handler("foo");
  19. echo "**** Testing gettype() and settype() functions ****\n";
  20. $fp = fopen(__FILE__, "r");
  21. $dfp = opendir( dirname(__FILE__) );
  22. $var1 = "another string";
  23. $var2 = array(2,3,4);
  24. class point
  25. {
  26. var $x;
  27. var $y;
  28. function point($x, $y) {
  29. $this->x = $x;
  30. $this->y = $y;
  31. }
  32. function __toString() {
  33. return "Object";
  34. }
  35. }
  36. $unset_var = 10;
  37. unset($unset_var);
  38. $values = array(
  39. array(1,2,3),
  40. $var1,
  41. $var2,
  42. 1,
  43. -20,
  44. 2.54,
  45. -2.54,
  46. NULL,
  47. false,
  48. "some string",
  49. 'string',
  50. $fp,
  51. $dfp,
  52. new point(10,20)
  53. );
  54. $types = array(
  55. "null",
  56. "integer",
  57. "int",
  58. "float",
  59. "double",
  60. "boolean",
  61. "bool",
  62. "resource",
  63. "array",
  64. "object",
  65. "string"
  66. );
  67. echo "\n*** Testing gettype(): basic operations ***\n";
  68. foreach ($values as $value) {
  69. var_dump( gettype($value) );
  70. }
  71. echo "\n*** Testing settype(): basic operations ***\n";
  72. foreach ($types as $type) {
  73. echo "\n-- Setting type of data to $type --\n";
  74. $loop_count = 1;
  75. foreach ($values as $var) {
  76. echo "-- Iteration $loop_count --\n"; $loop_count ++;
  77. // set to new type
  78. var_dump( settype($var, $type) );
  79. // dump the var
  80. var_dump( $var );
  81. // check the new type
  82. var_dump( gettype($var) );
  83. }
  84. }
  85. echo "Done\n";
  86. ?>