Check whether a binary string can be formed by concatenating given N numbers sequentially | Function that returns false if the number passed as argument contains digit ( s ) other than '0' or '1' ; Function that checks whether the binary string can be formed or not ; Empty string for storing the binary number ; check if a [ i ] can be a part of the binary string ; Conversion of int into string ; if a [ i ] can 't be a part  then break the loop ; possible to create binary string ; impossible to create binary string ; Driver code
< ? php function isBinary ( $ n ) { while ( $ n != 0 ) { $ temp = $ n % 10 ; if ( $ temp != 0 && $ temp != 1 ) { return false ; } $ n = intval ( $ n / 10 ) ; } return true ; } function formBinaryStr ( $ n , & $ a ) { $ flag = true ; $ s = " " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( isBinary ( $ a [ $ i ] ) ) $ s = $ s . strval ( $ a [ $ i ] ) ; else { $ flag = false ; break ; } } if ( $ flag ) echo $ s . " STRNEWLINE " ; else echo " - 1 STRNEWLINE " ; } $ a = array ( 10 , 1 , 0 , 11 , 10 ) ; $ N = sizeof ( $ a ) / sizeof ( $ a [ 0 ] ) ; formBinaryStr ( $ N , $ a ) ; ? >