1--TEST--
2Test fscanf() function: usage variations - float formats with strings
3--FILE--
4<?php
5
6/* Test fscanf() to scan strings using different float format types */
7
8$file_path = __DIR__;
9
10echo "*** Test fscanf(): different float format types with strings ***\n";
11
12// create a file
13$filename = "$file_path/fscanf_variation12.tmp";
14$file_handle = fopen($filename, "w");
15if($file_handle == false)
16  exit("Error:failed to open file $filename");
17
18// array of strings
19$strings = array (
20  "",
21  '',
22  "0",
23  '0',
24  "1",
25  '1',
26  "\x01",
27  '\x01',
28  "\01",
29  '\01',
30  'string',
31  "string",
32  "true",
33  "FALSE",
34  'false',
35  'TRUE',
36  "NULL",
37  'null'
38);
39
40$float_formats = array( "%f", "%hf", "%lf", "%Lf", " %f", "%f ", "% f", "\t%f", "\n%f", "%4f", "%30f", "%[0-9]", "%*f");
41
42$counter = 1;
43
44// writing to the file
45foreach($strings as $string) {
46  @fprintf($file_handle, $string);
47  @fprintf($file_handle, "\n");
48}
49// closing the file
50fclose($file_handle);
51
52// opening the file for reading
53$file_handle = fopen($filename, "r");
54if($file_handle == false) {
55  exit("Error:failed to open file $filename");
56}
57
58$counter = 1;
59// reading the values from file using different float formats
60foreach($float_formats as $float_format) {
61  // rewind the file so that for every foreach iteration the file pointer starts from bof
62  rewind($file_handle);
63  echo "\n-- iteration $counter --\n";
64  while( !feof($file_handle) ) {
65    try {
66      var_dump(fscanf($file_handle,$float_format));
67    } catch (ValueError $exception) {
68      echo $exception->getMessage() . "\n";
69    }
70  }
71  $counter++;
72}
73
74echo "\n*** Done ***";
75?>
76--CLEAN--
77<?php
78$file_path = __DIR__;
79$filename = "$file_path/fscanf_variation12.tmp";
80unlink($filename);
81?>
82--EXPECT--
83*** Test fscanf(): different float format types with strings ***
84
85-- iteration 1 --
86NULL
87NULL
88array(1) {
89  [0]=>
90  float(0)
91}
92array(1) {
93  [0]=>
94  float(0)
95}
96array(1) {
97  [0]=>
98  float(1)
99}
100array(1) {
101  [0]=>
102  float(1)
103}
104array(1) {
105  [0]=>
106  NULL
107}
108array(1) {
109  [0]=>
110  NULL
111}
112array(1) {
113  [0]=>
114  NULL
115}
116array(1) {
117  [0]=>
118  NULL
119}
120array(1) {
121  [0]=>
122  NULL
123}
124array(1) {
125  [0]=>
126  NULL
127}
128array(1) {
129  [0]=>
130  NULL
131}
132array(1) {
133  [0]=>
134  NULL
135}
136array(1) {
137  [0]=>
138  NULL
139}
140array(1) {
141  [0]=>
142  NULL
143}
144array(1) {
145  [0]=>
146  NULL
147}
148array(1) {
149  [0]=>
150  NULL
151}
152bool(false)
153
154-- iteration 2 --
155NULL
156NULL
157array(1) {
158  [0]=>
159  float(0)
160}
161array(1) {
162  [0]=>
163  float(0)
164}
165array(1) {
166  [0]=>
167  float(1)
168}
169array(1) {
170  [0]=>
171  float(1)
172}
173array(1) {
174  [0]=>
175  NULL
176}
177array(1) {
178  [0]=>
179  NULL
180}
181array(1) {
182  [0]=>
183  NULL
184}
185array(1) {
186  [0]=>
187  NULL
188}
189array(1) {
190  [0]=>
191  NULL
192}
193array(1) {
194  [0]=>
195  NULL
196}
197array(1) {
198  [0]=>
199  NULL
200}
201array(1) {
202  [0]=>
203  NULL
204}
205array(1) {
206  [0]=>
207  NULL
208}
209array(1) {
210  [0]=>
211  NULL
212}
213array(1) {
214  [0]=>
215  NULL
216}
217array(1) {
218  [0]=>
219  NULL
220}
221bool(false)
222
223-- iteration 3 --
224NULL
225NULL
226array(1) {
227  [0]=>
228  float(0)
229}
230array(1) {
231  [0]=>
232  float(0)
233}
234array(1) {
235  [0]=>
236  float(1)
237}
238array(1) {
239  [0]=>
240  float(1)
241}
242array(1) {
243  [0]=>
244  NULL
245}
246array(1) {
247  [0]=>
248  NULL
249}
250array(1) {
251  [0]=>
252  NULL
253}
254array(1) {
255  [0]=>
256  NULL
257}
258array(1) {
259  [0]=>
260  NULL
261}
262array(1) {
263  [0]=>
264  NULL
265}
266array(1) {
267  [0]=>
268  NULL
269}
270array(1) {
271  [0]=>
272  NULL
273}
274array(1) {
275  [0]=>
276  NULL
277}
278array(1) {
279  [0]=>
280  NULL
281}
282array(1) {
283  [0]=>
284  NULL
285}
286array(1) {
287  [0]=>
288  NULL
289}
290bool(false)
291
292-- iteration 4 --
293NULL
294NULL
295array(1) {
296  [0]=>
297  float(0)
298}
299array(1) {
300  [0]=>
301  float(0)
302}
303array(1) {
304  [0]=>
305  float(1)
306}
307array(1) {
308  [0]=>
309  float(1)
310}
311array(1) {
312  [0]=>
313  NULL
314}
315array(1) {
316  [0]=>
317  NULL
318}
319array(1) {
320  [0]=>
321  NULL
322}
323array(1) {
324  [0]=>
325  NULL
326}
327array(1) {
328  [0]=>
329  NULL
330}
331array(1) {
332  [0]=>
333  NULL
334}
335array(1) {
336  [0]=>
337  NULL
338}
339array(1) {
340  [0]=>
341  NULL
342}
343array(1) {
344  [0]=>
345  NULL
346}
347array(1) {
348  [0]=>
349  NULL
350}
351array(1) {
352  [0]=>
353  NULL
354}
355array(1) {
356  [0]=>
357  NULL
358}
359bool(false)
360
361-- iteration 5 --
362NULL
363NULL
364array(1) {
365  [0]=>
366  float(0)
367}
368array(1) {
369  [0]=>
370  float(0)
371}
372array(1) {
373  [0]=>
374  float(1)
375}
376array(1) {
377  [0]=>
378  float(1)
379}
380array(1) {
381  [0]=>
382  NULL
383}
384array(1) {
385  [0]=>
386  NULL
387}
388array(1) {
389  [0]=>
390  NULL
391}
392array(1) {
393  [0]=>
394  NULL
395}
396array(1) {
397  [0]=>
398  NULL
399}
400array(1) {
401  [0]=>
402  NULL
403}
404array(1) {
405  [0]=>
406  NULL
407}
408array(1) {
409  [0]=>
410  NULL
411}
412array(1) {
413  [0]=>
414  NULL
415}
416array(1) {
417  [0]=>
418  NULL
419}
420array(1) {
421  [0]=>
422  NULL
423}
424array(1) {
425  [0]=>
426  NULL
427}
428bool(false)
429
430-- iteration 6 --
431NULL
432NULL
433array(1) {
434  [0]=>
435  float(0)
436}
437array(1) {
438  [0]=>
439  float(0)
440}
441array(1) {
442  [0]=>
443  float(1)
444}
445array(1) {
446  [0]=>
447  float(1)
448}
449array(1) {
450  [0]=>
451  NULL
452}
453array(1) {
454  [0]=>
455  NULL
456}
457array(1) {
458  [0]=>
459  NULL
460}
461array(1) {
462  [0]=>
463  NULL
464}
465array(1) {
466  [0]=>
467  NULL
468}
469array(1) {
470  [0]=>
471  NULL
472}
473array(1) {
474  [0]=>
475  NULL
476}
477array(1) {
478  [0]=>
479  NULL
480}
481array(1) {
482  [0]=>
483  NULL
484}
485array(1) {
486  [0]=>
487  NULL
488}
489array(1) {
490  [0]=>
491  NULL
492}
493array(1) {
494  [0]=>
495  NULL
496}
497bool(false)
498
499-- iteration 7 --
500Bad scan conversion character " "
501Bad scan conversion character " "
502Bad scan conversion character " "
503Bad scan conversion character " "
504Bad scan conversion character " "
505Bad scan conversion character " "
506Bad scan conversion character " "
507Bad scan conversion character " "
508Bad scan conversion character " "
509Bad scan conversion character " "
510Bad scan conversion character " "
511Bad scan conversion character " "
512Bad scan conversion character " "
513Bad scan conversion character " "
514Bad scan conversion character " "
515Bad scan conversion character " "
516Bad scan conversion character " "
517Bad scan conversion character " "
518bool(false)
519
520-- iteration 8 --
521NULL
522NULL
523array(1) {
524  [0]=>
525  float(0)
526}
527array(1) {
528  [0]=>
529  float(0)
530}
531array(1) {
532  [0]=>
533  float(1)
534}
535array(1) {
536  [0]=>
537  float(1)
538}
539array(1) {
540  [0]=>
541  NULL
542}
543array(1) {
544  [0]=>
545  NULL
546}
547array(1) {
548  [0]=>
549  NULL
550}
551array(1) {
552  [0]=>
553  NULL
554}
555array(1) {
556  [0]=>
557  NULL
558}
559array(1) {
560  [0]=>
561  NULL
562}
563array(1) {
564  [0]=>
565  NULL
566}
567array(1) {
568  [0]=>
569  NULL
570}
571array(1) {
572  [0]=>
573  NULL
574}
575array(1) {
576  [0]=>
577  NULL
578}
579array(1) {
580  [0]=>
581  NULL
582}
583array(1) {
584  [0]=>
585  NULL
586}
587bool(false)
588
589-- iteration 9 --
590NULL
591NULL
592array(1) {
593  [0]=>
594  float(0)
595}
596array(1) {
597  [0]=>
598  float(0)
599}
600array(1) {
601  [0]=>
602  float(1)
603}
604array(1) {
605  [0]=>
606  float(1)
607}
608array(1) {
609  [0]=>
610  NULL
611}
612array(1) {
613  [0]=>
614  NULL
615}
616array(1) {
617  [0]=>
618  NULL
619}
620array(1) {
621  [0]=>
622  NULL
623}
624array(1) {
625  [0]=>
626  NULL
627}
628array(1) {
629  [0]=>
630  NULL
631}
632array(1) {
633  [0]=>
634  NULL
635}
636array(1) {
637  [0]=>
638  NULL
639}
640array(1) {
641  [0]=>
642  NULL
643}
644array(1) {
645  [0]=>
646  NULL
647}
648array(1) {
649  [0]=>
650  NULL
651}
652array(1) {
653  [0]=>
654  NULL
655}
656bool(false)
657
658-- iteration 10 --
659NULL
660NULL
661array(1) {
662  [0]=>
663  float(0)
664}
665array(1) {
666  [0]=>
667  float(0)
668}
669array(1) {
670  [0]=>
671  float(1)
672}
673array(1) {
674  [0]=>
675  float(1)
676}
677array(1) {
678  [0]=>
679  NULL
680}
681array(1) {
682  [0]=>
683  NULL
684}
685array(1) {
686  [0]=>
687  NULL
688}
689array(1) {
690  [0]=>
691  NULL
692}
693array(1) {
694  [0]=>
695  NULL
696}
697array(1) {
698  [0]=>
699  NULL
700}
701array(1) {
702  [0]=>
703  NULL
704}
705array(1) {
706  [0]=>
707  NULL
708}
709array(1) {
710  [0]=>
711  NULL
712}
713array(1) {
714  [0]=>
715  NULL
716}
717array(1) {
718  [0]=>
719  NULL
720}
721array(1) {
722  [0]=>
723  NULL
724}
725bool(false)
726
727-- iteration 11 --
728NULL
729NULL
730array(1) {
731  [0]=>
732  float(0)
733}
734array(1) {
735  [0]=>
736  float(0)
737}
738array(1) {
739  [0]=>
740  float(1)
741}
742array(1) {
743  [0]=>
744  float(1)
745}
746array(1) {
747  [0]=>
748  NULL
749}
750array(1) {
751  [0]=>
752  NULL
753}
754array(1) {
755  [0]=>
756  NULL
757}
758array(1) {
759  [0]=>
760  NULL
761}
762array(1) {
763  [0]=>
764  NULL
765}
766array(1) {
767  [0]=>
768  NULL
769}
770array(1) {
771  [0]=>
772  NULL
773}
774array(1) {
775  [0]=>
776  NULL
777}
778array(1) {
779  [0]=>
780  NULL
781}
782array(1) {
783  [0]=>
784  NULL
785}
786array(1) {
787  [0]=>
788  NULL
789}
790array(1) {
791  [0]=>
792  NULL
793}
794bool(false)
795
796-- iteration 12 --
797array(1) {
798  [0]=>
799  NULL
800}
801array(1) {
802  [0]=>
803  NULL
804}
805array(1) {
806  [0]=>
807  string(1) "0"
808}
809array(1) {
810  [0]=>
811  string(1) "0"
812}
813array(1) {
814  [0]=>
815  string(1) "1"
816}
817array(1) {
818  [0]=>
819  string(1) "1"
820}
821array(1) {
822  [0]=>
823  NULL
824}
825array(1) {
826  [0]=>
827  NULL
828}
829array(1) {
830  [0]=>
831  NULL
832}
833array(1) {
834  [0]=>
835  NULL
836}
837array(1) {
838  [0]=>
839  NULL
840}
841array(1) {
842  [0]=>
843  NULL
844}
845array(1) {
846  [0]=>
847  NULL
848}
849array(1) {
850  [0]=>
851  NULL
852}
853array(1) {
854  [0]=>
855  NULL
856}
857array(1) {
858  [0]=>
859  NULL
860}
861array(1) {
862  [0]=>
863  NULL
864}
865array(1) {
866  [0]=>
867  NULL
868}
869bool(false)
870
871-- iteration 13 --
872NULL
873NULL
874array(0) {
875}
876array(0) {
877}
878array(0) {
879}
880array(0) {
881}
882array(0) {
883}
884array(0) {
885}
886array(0) {
887}
888array(0) {
889}
890array(0) {
891}
892array(0) {
893}
894array(0) {
895}
896array(0) {
897}
898array(0) {
899}
900array(0) {
901}
902array(0) {
903}
904array(0) {
905}
906bool(false)
907
908*** Done ***
909