“Take a look at the function below. At first glance, what did you think she was doing? What is wrong with her?
Read the feature code carefully.
')
What potential shortcomings and inconveniences have you found in it?
How best to refactor or re-code the code? (if necessary, do it in your opinion). ”
<?
function choose_period ( $field_name = '' )
{
global $TEXT , $PHP_SELF , $PERIOD_TO , $PERIOD_FROM ;
if (!isset( $PERIOD_TO )) $PERIOD_TO = date ( "dmY" , time ());
if ( $PERIOD_TO ) {
ereg ( "(.+)\.(.+)\.(.+)" , $PERIOD_TO , $r );
$PERIOD_TO = mktime ( 23 , 59 , 59 , $r [ 2 ], $r [ 1 ], $r [ 3 ]);
}
if (!isset( $PERIOD_FROM ))
$PERIOD_FROM = date ( "dmY" , strtotime ( "-1 month +1day" , $PERIOD_TO ));
if ( $PERIOD_FROM ) {
ereg ( "(.+)\.(.+)\.(.+)" , $PERIOD_FROM , $r ); $PERIOD_FROM = mktime ( 0 , 0 , 0 , $r [ 2 ], $r [ 1 ], $r [ 3 ]);
}
$TEXT .= "<form action='$PHP_SELF'>" . $GLOBALS [ 'FORM' ]. "
c <input type=text name=PERIOD_FROM value='" .( $PERIOD_FROM ? date ( "dmY" , $PERIOD_FROM ) : "" ). "' size=12 title='" . date ( "dmY H:i:s" , $PERIOD_FROM ). "'>
<input type=text name=PERIOD_TO value='" .( $PERIOD_TO ? date ( "dmY" , $PERIOD_TO ): "" ). "' size=12 title='" . date ( "dmY H:i:s" , $PERIOD_TO ). "'>
<input class=b type=submit value='>>>'></form>" ;
if ( $field_name )
{
if ( $PERIOD_TO ) $WHERE .= " and $field_name<=$PERIOD_TO " ;
if ( $PERIOD_FROM ) $WHERE .= " and $field_name>=$PERIOD_FROM " ;
}
return eregi_replace ( "^ and " , " where " , $WHERE );
} ?>
Source: https://habr.com/ru/post/104356/
All Articles