CodeIgniter 常用函数
CodeIgniter 库函数和辅助函数在使用前需要初始化,但有一些常用函数不需要初始化。
下面给出了这些常用功能及其说明。
语法 |
is_php($version) |
Parameters |
$version (
string) − 版本号
|
Returns |
如果正在运行的 PHP 版本至少是指定的版本,则为 TRUE,否则为 FALSE |
Return Type |
void |
说明 |
确定正在使用的 PHP 版本是否大于提供的版本号。 |
语法 |
is_really_writable($file) |
Parameters |
$file (
string)-文件路径
|
Returns |
如果路径可写则为TRUE,否则为FALSE |
Return Type |
bool |
说明 |
检查文件是否可写。 |
语法 |
config_item($key) |
Parameters |
$key (
string) − 配置项键
|
Returns |
如果没有找到配置键值或NULL |
Return Type |
mixed |
说明 |
该函数用于获取配置项 |
语法 |
set_status_header($code[, $text = '']) |
Parameters |
$code (
int)-HTTP 响应状态码
$text (
string)-使用状态代码设置的自定义消息
|
Returns |
|
Return Type |
void |
说明 |
此功能允许您手动设置服务器状态标头。 |
语法 |
remove_invisible_characters($str[, $url_encoded = TRUE]) |
Parameters |
$str (
string)-输入字符串
$url_encoded (
bool)-是否也删除 URLencoded 字符
|
Returns |
清理过的字符串 |
Return Type |
String |
说明 |
此功能可防止在 ASCII 字符之间插入 NULL 字符 |
语法 |
html_escape($var) |
Parameters |
$var (
mixed)-要转义的变量(字符串或数组)
|
Returns |
HTML 转义字符串 |
Return Type |
mixed |
说明 |
此函数充当原生 PHP htmlspecialchars() 函数。 |
语法 |
get_mimes() |
Returns |
文件类型的关联数组 |
Return Type |
array |
说明 |
此函数返回对 application/config/mimes.php 中 MIME 数组的引用。 |
语法 |
is_https() |
Returns |
如果当前使用 HTTP-over-SSL,则为 TRUE,否则为 FALSE |
Return Type |
bool |
说明 |
如果使用安全 (HTTPS) 连接,则返回 TRUE,在任何其他情况下(包括非 HTTP 请求)返回 FALSE。 |
语法 |
is_cli() |
Returns |
如果当前在 CLI 下运行,则为 TRUE,否则为 FALSE |
Return Type |
bool |
说明 |
如果应用程序通过命令行运行,则返回 TRUE,否则返回 FALSE。 |
语法 |
function_usable($function_name) |
Parameters |
$function_name (
string) − 函数名
|
Return Type |
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