CodeIgniter - 常用函数
CodeIgniter 库函数和辅助函数在使用前需要初始化,但有些常用函数不需要初始化。
这些常用函数及其说明如下。
语法 | is_php($version) |
---|---|
参数 |
$version (string) −版本号 |
返回 | 如果正在运行的 PHP 版本至少为指定的版本,则返回 TRUE,否则返回 FALSE |
返回类型 | void |
描述 | 确定正在使用的 PHP 版本是否大于提供的版本号。 |
语法 | is_really_writable($file) |
---|---|
参数 |
$file (string) −文件路径 |
返回 | 如果路径可写则返回 TRUE,否则返回 FALSE |
返回类型 | bool |
描述 | 检查文件是否可写。 |
语法 | config_item($key) |
---|---|
参数 |
$key (string) −配置项键 |
返回 | 配置键值,若未找到则返回 NULL |
返回类型 | mixed |
说明 | 此函数用于获取配置项 |
语法 | set_status_header($code[, $text = '']) |
---|---|
参数 |
$code (int) − HTTP 响应状态代码 $text (string) −使用状态代码设置的自定义消息 |
返回 | |
返回类型 | void |
描述 | 此函数允许您手动设置服务器状态标头。 |
语法 | remove_invisible_characters($str[, $url_encoded = TRUE]) |
---|---|
参数 |
$str (string) − 输入字符串 $url_encoded (bool) −是否同时删除 URL 编码字符 |
返回 | 清理后的字符串 |
返回类型 | 字符串 |
描述 | 此函数可防止在 ASCII 字符之间插入 NULL 字符 |
语法 | html_escape($var) |
---|---|
参数 |
$var (mixed) −要转义的变量(字符串或数组) |
返回 | HTML 转义字符串 |
返回类型 | 混合 |
描述 | 此函数充当本机 PHP htmlspecialchars() 函数。 |
语法 | get_mimes() |
---|---|
返回 | 文件类型的关联数组 |
返回类型 | array |
说明 | 此函数返回对 application/config/mimes.php 中 MIMEs 数组的引用。 |
语法 | is_https() |
---|---|
返回 | 如果当前正在使用 HTTP-over-SSL,则为 TRUE,否则为 FALSE |
返回类型 | bool |
说明 | 返回如果使用安全 (HTTPS) 连接则为 TRUE,其他情况下(包括非 HTTP 请求)则为 FALSE。 |
语法 | is_cli() |
---|---|
返回 | 如果当前在 CLI 下运行,则返回 TRUE,否则返回 FALSE |
返回类型 | bool |
描述 | 如果应用程序通过命令行运行,则返回 TRUE,否则返回 FALSE。 |
语法 | function_usable($function_name) |
---|---|
参数 |
$function_name (string) −函数名称 |
返回类型 | bool |
描述 | 如果函数存在且可用,则返回 TRUE,否则返回 FALSE。 |
下面给出了一个示例,演示了上述所有函数。
示例
这里我们只创建了一个控制器,我们将在其中使用上述函数。复制下面给出的代码并将其保存在 application/controller/CommonFun_Controller.php。
<?php class CommonFun_Controller extends CI_Controller { public function index() { set_status_header(200); echo is_php('5.3')."<br>"; var_dump(is_really_writable('./Form.php')); echo config_item('language')."<br>"; echo remove_invisible_characters('This is a test','UTF8')."<br>"; $str = '< This > is \' a " test & string'; echo html_escape($str)."<br>"; echo "is_https():".var_dump(is_https())."<br>"; echo "is_cli():".var_dump(is_cli())."<br>"; var_dump(function_usable('test'))."<br>"; echo "get_mimes():".print_r(get_mimes())."<br>"; } public function test() { echo "Test function"; } } ?>
更改 application/config/routes.php 中的 routes.php 文件,为上述控制器添加路由,并在文件末尾添加以下行。
$route['commonfunctions'] = 'CommonFun_Controller';
在浏览器的地址栏中输入以下 URL 以执行示例。
http://yoursite.com/index.php/commonfunctions