Let’s say I would like to have a PHP line:
image( "src => images/logo.png", "class => right", "alt => Company Logo" ); |
to generate a full blown HTML <img> tag:
<img src="http://www.dotkam.com/images/company-logo.png" class="right" alt="Company Logo"> |
For this I would need to translate these three function parameters:
"src => images/logo.png", "class => right", "alt => Company Logo" |
into a MAP ( yes, PHP calls it an array, but it is in fact a MAP ):
imageTag ( 'src' => 'images/logo.png' 'class' => 'right' 'alt' => 'Company Logo' ) |
Here is how it is done:
function image( ) { $validAttributes = array( 'src', 'class', 'alt' ); foreach ( func_get_args() as $attr ) { $attrMap = explode( ' => ', $attr ); if ( in_array( $attrMap[0], $validAttributes ) ) { $imageAttrMap[ $attrMap[0] ] = $attrMap[1]; } else { die( 'Configuration Problem: ['.$attrMap[0].'] is not a valid <img> attribute.' ); } } // .... validate 'src' is there, append imgTag string with all the attrs, echo it... } |