1--TEST-- 2Test file_put_contents() function : usage variation - different data types to write 3--CREDITS-- 4Dave Kelsey <d_kelsey@uk.ibm.com> 5--FILE-- 6<?php 7/* Prototype : int file_put_contents(string file, mixed data [, int flags [, resource context]]) 8 * Description: Write/Create a file with contents data and return the number of bytes written 9 * Source code: ext/standard/file.c 10 * Alias to functions: 11 */ 12 13echo "*** Testing file_put_contents() : usage variation ***\n"; 14 15// Define error handler 16function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { 17 if (error_reporting() != 0) { 18 // report non-silenced errors 19 echo "Error: $err_no - $err_msg, $filename($linenum)\n"; 20 } 21} 22set_error_handler('test_error_handler'); 23 24// Initialise function arguments not being substituted (if any) 25 26$filename = __DIR__ . '/fwriteVar5.tmp'; 27 28 29 30//get an unset variable 31$unset_var = 10; 32unset ($unset_var); 33 34// define some classes 35class classWithToString 36{ 37 public function __toString() { 38 return "Class A object"; 39 } 40} 41 42class classWithoutToString 43{ 44} 45 46// heredoc string 47$heredoc = <<<EOT 48hello world 49EOT; 50 51// add arrays 52$index_array = array (1, 2, 3); 53$assoc_array = array ('one' => 1, 'two' => 2); 54 55//array of values to iterate over 56$inputs = array( 57 58 // int data 59 'int 0' => 0, 60 'int 1' => 1, 61 'int 12345' => 12345, 62 'int -12345' => -2345, 63 64 // float data 65 'float 10.5' => 10.5, 66 'float -10.5' => -10.5, 67 'float 12.3456789000e10' => 12.3456789000e10, 68 'float -12.3456789000e10' => -12.3456789000e10, 69 'float .5' => .5, 70 71 // array data 72 'empty array' => array(), 73 'int indexed array' => $index_array, 74 'associative array' => $assoc_array, 75 'nested arrays' => array('foo', $index_array, $assoc_array), 76 77 // null data 78 'uppercase NULL' => NULL, 79 'lowercase null' => null, 80 81 // boolean data 82 'lowercase true' => true, 83 'lowercase false' =>false, 84 'uppercase TRUE' =>TRUE, 85 'uppercase FALSE' =>FALSE, 86 87 // empty data 88 'empty string DQ' => "", 89 'empty string SQ' => '', 90 91 // object data 92 'instance of classWithToString' => new classWithToString(), 93 'instance of classWithoutToString' => new classWithoutToString(), 94 95 // undefined data 96 'undefined var' => @$undefined_var, 97 98 // unset data 99 'unset var' => @$unset_var, 100); 101 102// loop through each element of the array for str 103 104foreach($inputs as $key =>$value) { 105 echo "\n--$key--\n"; 106 file_put_contents($filename, $value); 107 readfile($filename); 108}; 109unlink($filename); 110 111?> 112===DONE=== 113--EXPECTF-- 114*** Testing file_put_contents() : usage variation *** 115 116--int 0-- 1170 118--int 1-- 1191 120--int 12345-- 12112345 122--int -12345-- 123-2345 124--float 10.5-- 12510.5 126--float -10.5-- 127-10.5 128--float 12.3456789000e10-- 129123456789000 130--float -12.3456789000e10-- 131-123456789000 132--float .5-- 1330.5 134--empty array-- 135 136--int indexed array-- 137123 138--associative array-- 13912 140--nested arrays-- 141Error: 8 - Array to string conversion, %s(%d) 142Error: 8 - Array to string conversion, %s(%d) 143fooArrayArray 144--uppercase NULL-- 145 146--lowercase null-- 147 148--lowercase true-- 1491 150--lowercase false-- 151 152--uppercase TRUE-- 1531 154--uppercase FALSE-- 155 156--empty string DQ-- 157 158--empty string SQ-- 159 160--instance of classWithToString-- 161Class A object 162--instance of classWithoutToString-- 163 164--undefined var-- 165 166--unset var-- 167===DONE=== 168