当一个论点没有起作用时该怎么办?

时间:2017-04-17 13:04:17

标签: php codeigniter-3

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

if ( ! function_exists('image_thumb')){
    function image_thumb($courseBanner, $userId){
        $CI =& get_instance();
        $filename_ext = pathinfo($courseBanner, PATHINFO_EXTENSION);
        $course_thumb_Banner = preg_replace('/^(.*)\.' . $filename_ext . '$/', '$1_thumb.' . $filename_ext, $courseBanner);
        $url =  base_url()."private/".$userId."/".$course_thumb_Banner;
        $headers=get_headers($url);
        $is_banner_there  =  stripos($headers[0],"200 OK")?true:false;
        $url = ($is_banner_there)?$url: base_url()."public/images/placeholder.jpg";
        return $url;
    }
}

在上面的代码中,有时$ courseBanner没有来,所以我想发送这个base_url()."public/images/placeholder.jpg怎么办?

1 个答案:

答案 0 :(得分:1)

建议:

我认为如果字符串(例如$ courseBanner)为空,您可以设置默认值

示例:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('image_thumb')){
    function image_thumb($userId, $courseBanner = ''){
        $CI =& get_instance();
        $courseBanner = !empty($courseBanner) ? $courseBanner : base_url()."public/images/placeholder.jpg";
        $filename_ext = pathinfo($courseBanner, PATHINFO_EXTENSION);
        $course_thumb_Banner = preg_replace('/^(.*)\.' . $filename_ext . '$/', '$1_thumb.' . $filename_ext, $courseBanner);
        $url =  base_url()."private/".$userId."/".$course_thumb_Banner;
        $headers=get_headers($url);
        $is_banner_there  =  stripos($headers[0],"200 OK")?true:false;
        return $url;
    }
}

这仅供参考,希望对此有所帮助