刚学PHP,写个函数用来绘制img标签,,但报错,求解~function draw_img($src,$class,$id,$other,$insert){
$str='<img src="'.$src.'" ';
if (empty($class))$class='class="'.$class.'" ';
if (empty($id))$id='id="'.$id.'" ';
if (empty($other))$other='other="'.$other.'" ';
if (empty($insert))$insert='insert="'.$insert.'" ';
$str.=$class.$id.$other.'>'.$insert.'</img>';
return $str;
}
echo draw_img(MYIMAGESPATH.'icons/E-mail.png';)
错误内容:
Parse error: syntax error, unexpected ';' in F:\PHP\www\htdocs\MyLife\Index.php on line 39
$str='<img src="'.$src.'" ';
if (empty($class))$class='class="'.$class.'" ';
if (empty($id))$id='id="'.$id.'" ';
if (empty($other))$other='other="'.$other.'" ';
if (empty($insert))$insert='insert="'.$insert.'" ';
$str.=$class.$id.$other.'>'.$insert.'</img>';
return $str;
}
echo draw_img(MYIMAGESPATH.'icons/E-mail.png';)
错误内容:
Parse error: syntax error, unexpected ';' in F:\PHP\www\htdocs\MyLife\Index.php on line 39
Fatal error: Call to undefined function draw_img() in F:\PHP\www\htdocs\MyLife\Index.php on line 39代码echo draw_img(MYIMAGESPATH.'icons/E-mail.png');去掉echo也是一样
function draw_img($src,$class='',$id='',$other='',$insert='')
{
$str='<img src="'.$src.'" ';
if (empty($class))$class='class="'.$class.'" ';
if (empty($id))$id='id="'.$id.'" ';
if (empty($other))$other='other="'.$other.'" ';
if (empty($insert))$insert='insert="'.$insert.'" ';
$str.=$class.$id.$other.'>'.$insert.'</img>';
return $str;
}
echo draw_img(MYIMAGESPATH.'icons/E-mail.png');
{
$str='<img src="'.$src.'" ';
if (empty($class))$class='class="'.$class.'" ';
if (empty($id))$id='id="'.$id.'" ';
if (empty($other))$other='other="'.$other.'" ';
if (empty($insert))$insert='insert="'.$insert.'" ';
$str.=$class.$id.$other.'>'.$insert.'</img>';
return $str;
}
echo draw_img(MYIMAGESPATH.'icons/E-mail.png');
在传参上加上默认值function draw_img($src='',$class='',$id='',$other='',$insert='')
$str = "<img src='$src' ";
$class = !empty($class)? "class='$class' ":'';
$id = !empty($id)? "id='$id' ":'';
$other = !empty($other)? "other='$other' ":'';
$insert = !empty($insert)? "insert='$insert' ":'';
$str.=$class.$id.$other." />"; //img是单标签。注意单引号和双引号的区别。
echo $str;
}
echo draw_img('/img.jpg','on');
function draw_img($src,$class,$url='',$width='',$height=''){ //图片地址,样式,链接的地址,宽度,高度
$str = "<img src='$src' ";
$class = !empty($class)? "class='$class' ":'';
$id = !empty($id)? "id='$id' ":'';
$width = !empty($width)? "width='$width' ":'';
$height = !empty($height)? "height='$height' ":'';
$str.=$class.$url.$width.$height." />"; //img是单标签。注意单引号和双引号的区别。
echo $str;
}
echo draw_img('/img.jpg','on','http://www.csdn.net/','200','150'); //
function draw_img($src,$class,$alt='',$width='',$height=''){ //图片地址,样式,链接的地址,宽度,高度
$str = "<img src='$src' ";
$class = !empty($class)? "class='$class' ":'';
$alt = !empty($alt)? "alt='$alt' ":'';
$width = !empty($width)? "width='$width' ":'';
$height = !empty($height)? "height='$height' ":'';
$str.=$class.$alt.$width.$height." />"; //img是单标签。注意单引号和双引号的区别。
echo $str;
}
echo draw_img('/img.jpg','on','无法显示后显示的内容','200','150'); //