1--TEST-- 2sprintf() formats with different types 3--FILE-- 4<?php 5 6$formats = ['s', 'd', 'u', 'f', 'c', 'x']; 7$values = [null, false, true, 2, 3.5, "foo", [], [1], fopen(__FILE__, "r"), new stdClass]; 8 9foreach ($formats as $format) { 10 foreach ($values as $value) { 11 echo "$format with " . (is_resource($value) ? "resource" : json_encode($value)) . ":\n"; 12 try { 13 echo sprintf("%" . $format, $value), "\n"; 14 } catch (Error $e) { 15 echo $e->getMessage(), "\n"; 16 } 17 echo "\n"; 18 } 19} 20 21?> 22--EXPECTF-- 23%s with null: 24 25 26%s with false: 27 28 29%s with true: 301 31 32%s with 2: 332 34 35s with 3.5: 363.5 37 38%s with "foo": 39foo 40 41%s with []: 42 43Warning: Array to string conversion in %s on line %d 44Array 45 46%s with [1]: 47 48Warning: Array to string conversion in %s on line %d 49Array 50 51%s with resource: 52Resource id #%d 53 54%s with {}: 55Object of class stdClass could not be converted to string 56 57d with null: 580 59 60d with false: 610 62 63d with true: 641 65 66d with 2: 672 68 69d with 3.5: 703 71 72d with "foo": 730 74 75d with []: 760 77 78d with [1]: 791 80 81d with resource: 82%d 83 84d with {}: 85 86Warning: Object of class stdClass could not be converted to int in %s on line %d 871 88 89u with null: 900 91 92u with false: 930 94 95u with true: 961 97 98u with 2: 992 100 101u with 3.5: 1023 103 104u with "foo": 1050 106 107u with []: 1080 109 110u with [1]: 1111 112 113u with resource: 114%d 115 116u with {}: 117 118Warning: Object of class stdClass could not be converted to int in %s on line %d 1191 120 121f with null: 1220.000000 123 124f with false: 1250.000000 126 127f with true: 1281.000000 129 130f with 2: 1312.000000 132 133f with 3.5: 1343.500000 135 136f with "foo": 1370.000000 138 139f with []: 1400.000000 141 142f with [1]: 1431.000000 144 145f with resource: 146%d.000000 147 148f with {}: 149 150Warning: Object of class stdClass could not be converted to float in %s on line %d 1511.000000 152 153c with null: 154%0 155 156c with false: 157%0 158 159c with true: 160 161 162c with 2: 163 164 165c with 3.5: 166 167 168c with "foo": 169%0 170 171c with []: 172%0 173 174c with [1]: 175 176 177c with resource: 178%s 179 180c with {}: 181 182Warning: Object of class stdClass could not be converted to int in %s on line %d 183 184 185x with null: 1860 187 188x with false: 1890 190 191x with true: 1921 193 194x with 2: 1952 196 197x with 3.5: 1983 199 200x with "foo": 2010 202 203x with []: 2040 205 206x with [1]: 2071 208 209x with resource: 210%d 211 212x with {}: 213 214Warning: Object of class stdClass could not be converted to int in %s on line %d 2151 216