Generate all the binary strings of N bits | Function to print the output ; Function to generate all binary strings ; First assign "0" at ith position and try for all other permutations for remaining positions ; And then assign "1" at ith position and try for all other permutations for remaining positions ; Driver Code ; Print all binary strings
< ? php function printTheArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo $ arr [ $ i ] , " " ; } echo " STRNEWLINE " ; } function generateAllBinaryStrings ( $ n , $ arr , $ i ) { if ( $ i == $ n ) { printTheArray ( $ arr , $ n ) ; return ; } $ arr [ $ i ] = 0 ; generateAllBinaryStrings ( $ n , $ arr , $ i + 1 ) ; $ arr [ $ i ] = 1 ; generateAllBinaryStrings ( $ n , $ arr , $ i + 1 ) ; } $ n = 4 ; $ arr = array_fill ( 0 , $ n , 0 ) ; generateAllBinaryStrings ( $ n , $ arr , 0 ) ; ? >