刚学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

解决方案 »

  1.   

    echo draw_img(MYIMAGESPATH.'icons/E-mail.png'); 分号写到外面来
      

  2.   

    还是报错..变成
    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也是一样
      

  3.   


    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');
      

  4.   

    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');
    在传参上加上默认值function draw_img($src='',$class='',$id='',$other='',$insert='')
      

  5.   

    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');
      

  6.   

    谢谢..然后发现我忘了在empty函数前加!号了,呵呵~我以前是VB的,没习惯类似C那些书写
      

  7.   

    我又稍微改了一下。你看一下。我也是第一次写这个。嘻嘻。。
    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'); //
      

  8.   

    不仔细看 错误百出呀。呵呵。我也学习学习。
    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'); //