Printing string in plus ‘ + ’ pattern in the matrix | Function to make a cross in the matrix ; As , it is not possible to make the cross exactly in the middle of the matrix with an even length string . ; declaring a 2D array i . e a matrix ; Now , we will fill all the elements of the array with ' X ' ; Now , we will place the characters of the string in the matrix , such that a cross is formed in it . ; here the characters of the string will be added in the middle column of our array . ; here the characters of the string will be added in the middle row of our array . ; Now finally , we will print the array ; Driver Code
< ? php function carveCross ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n % 2 == 0 ) { echo ( " Not ▁ possible . ▁ Please ▁ enter ▁ " ) ; echo ( " odd ▁ length ▁ string . STRNEWLINE " ) ; } else { $ arr = array ( ) ; $ m = $ n / 2 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ arr [ $ i ] [ $ j ] = ' X ' ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arr [ $ i ] [ $ m ] = $ str [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arr [ $ m ] [ $ i ] = $ str [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { echo ( $ arr [ $ i ] [ $ j ] . " " ) ; } echo ( " STRNEWLINE " ) ; } } } $ str = " PICTURE " ; carveCross ( $ str ) ; ? >