1--TEST-- 2Test strncmp() function : usage variations - different lengths(all types) 3--SKIPIF-- 4<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64-bit only"); 5--FILE-- 6<?php 7/* Prototype : int strncmp ( string $str1, string $str2, int $len ); 8 * Description: Binary safe case-sensitive string comparison of the first n characters 9 * Source code: Zend/zend_builtin_functions.c 10*/ 11 12/* Test strncmp() function with the length as all types, and giving the same strings for 'str1' and 'str2' */ 13 14echo "*** Test strncmp() function: by supplying all types for 'len' ***\n"; 15 16/* definition of required variables */ 17$str1 = "Hello, World\n"; 18$str2 = "Hello, World\n"; 19 20/* get an unset variable */ 21$unset_var = 'string_val'; 22unset($unset_var); 23 24/* get resource handle */ 25$file_handle = fopen(__FILE__, "r"); 26 27/* declaring a class */ 28class sample { 29 public function __toString() { 30 return "object"; 31 } 32} 33 34 35/* array with different values */ 36$lengths = array ( 37 /* integer values */ 38 0, 39 1, 40 12345, 41 42 /* float values */ 43 10.5, 44 10.5e10, 45 10.6E-10, 46 .5, 47 48 /* hexadecimal values */ 49 0x12, 50 51 /* octal values */ 52 012, 53 01.2, 54 55 /* array values */ 56 array(), 57 array(0), 58 array(1), 59 array(1, 2), 60 array('color' => 'red', 'item' => 'pen'), 61 62 /* boolean values */ 63 true, 64 false, 65 TRUE, 66 FALSE, 67 68 /* nulls */ 69 NULL, 70 null, 71 72 /* empty string */ 73 "", 74 '', 75 76 /* undefined variable */ 77 $undefined_var, 78 79 /* unset variable */ 80 $unset_var, 81 82 /* resource */ 83 $file_handle, 84 85 /* object */ 86 new sample() 87); 88 89/* loop through each element of the array and check the working of strncmp() */ 90$counter = 1; 91for($index = 0; $index < count($lengths); $index ++) { 92 $len = $lengths[$index]; 93 echo "-- Iteration $counter --\n"; 94 var_dump( strncmp($str1, $str2, $len) ); 95 $counter ++; 96} 97fclose($file_handle); 98 99echo "*** Done ***\n"; 100?> 101--EXPECTF-- 102*** Test strncmp() function: by supplying all types for 'len' *** 103 104Notice: Undefined variable: undefined_var in %s on line %d 105 106Notice: Undefined variable: unset_var in %s on line %d 107-- Iteration 1 -- 108int(0) 109-- Iteration 2 -- 110int(0) 111-- Iteration 3 -- 112int(0) 113-- Iteration 4 -- 114int(0) 115-- Iteration 5 -- 116int(0) 117-- Iteration 6 -- 118int(0) 119-- Iteration 7 -- 120int(0) 121-- Iteration 8 -- 122int(0) 123-- Iteration 9 -- 124int(0) 125-- Iteration 10 -- 126int(0) 127-- Iteration 11 -- 128 129Warning: strncmp() expects parameter 3 to be integer, array given in%s on line %d 130NULL 131-- Iteration 12 -- 132 133Warning: strncmp() expects parameter 3 to be integer, array given in%s on line %d 134NULL 135-- Iteration 13 -- 136 137Warning: strncmp() expects parameter 3 to be integer, array given in%s on line %d 138NULL 139-- Iteration 14 -- 140 141Warning: strncmp() expects parameter 3 to be integer, array given in%s on line %d 142NULL 143-- Iteration 15 -- 144 145Warning: strncmp() expects parameter 3 to be integer, array given in%s on line %d 146NULL 147-- Iteration 16 -- 148int(0) 149-- Iteration 17 -- 150int(0) 151-- Iteration 18 -- 152int(0) 153-- Iteration 19 -- 154int(0) 155-- Iteration 20 -- 156int(0) 157-- Iteration 21 -- 158int(0) 159-- Iteration 22 -- 160 161Warning: strncmp() expects parameter 3 to be integer, string given in%s on line %d 162NULL 163-- Iteration 23 -- 164 165Warning: strncmp() expects parameter 3 to be integer, string given in%s on line %d 166NULL 167-- Iteration 24 -- 168int(0) 169-- Iteration 25 -- 170int(0) 171-- Iteration 26 -- 172 173Warning: strncmp() expects parameter 3 to be integer, resource given in%s on line %d 174NULL 175-- Iteration 27 -- 176 177Warning: strncmp() expects parameter 3 to be integer, object given in%s on line %d 178NULL 179*** Done *** 180