rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » PHP

PHP Functions

1。 PHP入门:define的使用:

PHP预先定义了几个常量,并提供了一种机制在运行时自己定义。常量和变量基本上是一样的,不同的是:常量必须用DEFINE函数定义,常量一旦定义好,就不能被重新定义了

PHP中预先定义好的常量:

__FILE__

当前正在处理的脚本文件名。如果使用在一个被包含的文件中,那么它的值就是这个被包含的文件,而不是包含它的文件名。

__LINE__

正在处理的文件的当前行数。

PHP_VERSION

表示PHP处理器的当前版本,如:'3.0.8-dev'。

PHP_OS

PHP处理器所在的操作系统名字,如:'Linux'。

TRUE

真值

FALSE

假值

可以用DEFINE函数定义更多的常量。

如,定义常量:

<?php

       define("CONSTANT", "Hello world.");

       echo CONSTANT; // Hello world.

       print("<br/>");

       DEFINE('XIBO', true);

       echo XIBO;   // 1

       print("<br/>");

       defined('XIBO') or die("Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.");

?>

用 __FILE__ 和 __LINE__ 的举例

<?php

// example of using __FILE__ and __LINE__

function report_error($file, $line, $message)

{

       echo "An error occured in $file on line $line: $message.";

}

// output: An error occured in C:\xampp\htdocs\HisProject\PhpInfo.php on line 65: Something went wrong!.

report_error(__FILE__,__LINE__, "Something went wrong!");

print("<br/>");

?>

From: http://www.xgdown.com/article/8/141245_1.htm

2。PHP: include_once - Manual:

http://www.blabla.cn/z/php_cn/function.include-once.html

include_once()

The include_once() 语句在脚本执行期间包含并运行指定文件。此行为和 include() 语句类似,唯一区别是如果该文件中的代码已经被包含了,则不会再次包含。如同此语句名字暗示的那样,只会包含一次。

include_once() 应该用于在脚本执行期间 同一个文件有可能被包含超过一次的情况下,想确保它只被包含一次以避免函数重定义,变量重新赋值等问题

返回值和 include() 相同。如果文件已被包含,本函数返回 TRUE

Note: include_once() 是 PHP 4.0.1pl2 中新加入的。

Note: 要注意 include_once() 和 require_once() 在大小写不敏感的操作系统中(例如 Windows)的行为可能不是所期望的。

Example#1 include_once() 在 Windows 下不区分大小写

<?php

       include_once( "a.php" );  // this will include a.php

       include_once( "A.php" );  // this will include a.php again on Windows! (PHP 4 only)

?>

此行为在 PHP 5 中改了,路径先被规格化,因此 C:\PROGRA~1\A.php 和 C:\Program Files\a.php 的实现一样,文件只会被包含一次。

3。php defined() 函数:

http://www.fzs8.net/php/php_tutorial/2007-03-18/1070.html

Definition and Usage
定义和用法

The defined() function checks whether a constant exists.
defined()函数的作用是:检查一个常量是否存在。

Returns TRUE if the constant exists, or FALSE otherwise.
如果该常量存在,则返回True;如果不存在,则返回False。

Syntax
语法

defined(name)

Parameter
参数

Description
描述

name

Required. Specifies the name of the constant to check
必要参数。指定常量对象的名称

Example
案例

<?php

       define("GREETING", "Hello you! How are you today?");

       echo defined("GREETING");  // outputs: 1

?>

The output of the code above will be:
上述代码将输出下面的结果:

4。PHP error_reporting() 函数:

http://www.w3school.com.cn/php/func_error_reporting.asp

定义和用法

error_reporting() 设置 PHP 的报错级别并返回当前级别。

语法

error_reporting(report_level)

如果参数 level 未指定,当前报错级别将被返回。下面几项是 level 可能的值:

E_ERROR, E_WARNING, ……

例子

任意数目的以上选项都可以用“或”来连接(用 OR 或 |),这样可以报告所有需要的各级别错误。例如,下面的代码关闭了用户自定义的错误和警告,执行了某些操作,然后恢复到原始的报错级别:

<?php

//禁用错误报告

error_reporting(0);

//报告运行时错误

error_reporting(E_ERROR | E_WARNING | E_PARSE);

//报告所有错误

error_reporting(E_ALL);

?>

5。php 中 ini_set 函数使用方法:

http://www.phperz.com/php/php-article/1030159420081594.html

ini_set 是php自带的用来 设置php.ini配置文件的函数

语法: ini_set("选项","值")

该函数用时最好放到php的脚本最头部

比如: ini_set(''max_execution_time'', ''180'');  // 设置php的脚本超时时间为180秒

ini_set("asp_tags","On")  // 打开asp脚本标记的支持比如:<% echo "aaa"%>

ini_set("display_errors","On") // 打脚本错误信息 等等

具体php选项可参考phpinfo文件的 Configuration PHP Core 部分

6。函数:get_magic_quotes_gpc():

http://www.phpv.net/html/699.html

取得 PHP 环境变量 magic_quotes_gpc 的值。

语法: long get_magic_quotes_gpc(void);

返回值: 长整数

函数种类: PHP 系统功能

内容说明

本函数取得 PHP 环境配置的变量 magic_quotes_gpc (GPC, Get/Post/Cookie) 值。返回 0 表示关闭本功能;返回 1 表示本功能打开。当 magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), \ (反斜线) and 空字符会自动转为含有反斜线的溢出字符

7。PHP  is_array():

http://www.fzs8.net/php/php_tutorial/2007-07-01/7231.html

is_array

(PHP 3, PHP 4 )

is_array -- 检测变量是否是数组

描述

bool is_array ( mixed var)

如果 var 是 array,则返回 TRUE,否则返回 FALSE

8。PHP  array_map() 函数:

http://www.w3school.com.cn/php/func_array_map.asp

定义和用法

array_map() 函数返回用户自定义函数作用后的数组。回调函数接受的参数数目应该和传递给 array_map() 函数的数组数目一致。

语法

array_map(function,array1,array2,array3...)

参数

描述

function

必需。用户自定义函数的名称,或者是 null。

array1

必需。规定数组。

array2

可选。规定数组。

array3

可选。规定数组。

例子 1

<?php

function myfunction($v)

       if ($v=="Dog")

       {

              return "Fido";

       }

       return $v;

$a=array("Horse","Dog","Cat");

print_r(array_map("myfunction",$a));

?>

输出:

Array ( [0] => Horse [1] => Fido [2] => Cat )

例子 2

使用多个参数:

<?php

function myfunction($v1,$v2)

       if ($v1===$v2)

       {

              return "same";

       }

       return "different";

$a1=array("Horse","Dog","Cat");

$a2=array("Cow","Dog","Rat");

print_r(array_map("myfunction",$a1,$a2));

?>

输出:

Array ( [0] => different [1] => same [2] => different )

例子 3

请看当自定义函数名设置为 null 时的情况:

<?php

       $a1=array("Dog","Cat");

       $a2=array("Puppy","Kitten");

       print_r(array_map(null,$a1,$a2));

?>

输出:

Array (

[0] => Array ( [0] => Dog [1] => Puppy )

[1] => Array ( [0] => Cat [1] => Kitten )

9。PHP stripslashes() 函数:

http://www.w3school.com.cn/php/func_string_stripslashes.asp

定义和用法

stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。

语法


stripslashes(string)

参数

描述

string

必需。规定要检查的字符串。

提示和注释

注释:该函数用于清理从数据库或 HTML 表单中取回的数据。

例子


<?php

               echo stripslashes("Who\'s John Adams?");

?>

输出:


Who's John Adams?

10。php die() 函数

http://www.fzs8.net/php/php_tutorial/2007-03-18/1071.html

Definition and Usage
定义和用法

The die() function prints a message and exits the current script.
die()函数的作用是:退出当前脚本程序并输出一段信息。

This function is an alias of the exit() function.
die()函数与exit()函数的功能大致相同。

Syntax
语法

die(message)

Parameter参数

Description描述

message

Required. Specifies the message or status number to write before exiting the script. The status number will not be written to the output.
必要参数。在退出脚本程序运行之前指定需要输出的信息和表示状态的数字[status number]。这个状态数字将不会被写入结果中

Example
案例

<?php $site = "http://www.w3schools.com/";

         fopen($site,"r")or die("Unable to connect to $site");

?>

11。PHP中include()与require()的区别 :

http://www.phpweblog.net/yemoo/archive/2007/09/29/2046.html

很久没碰过php了,连一些基础的东西都忘得差不多,今日看php文档,看到include和require区别的讲解,感觉这对于初学者是一个很容易迷惑的地方,在此记录下来,以便自己查阅,也希望对各位php朋友有所帮助。

引用文件的方法有两种:require 及 include。两种方式提供不同的使用弹性。

require 的使用方法如 require("MyRequireFile.php"); 。这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指定引入的文件,使它变成 PHP 程序网页的一部份。常用的函数,亦可以通过这个方法将它引入网页中。

include 使用方法如 include("MyIncludeFile.php"); 。这个函数一般是放在流程控制的处理部分中。PHP 程序网页在读到 include 的文件时,才将它读进来。这种方式,可以把程序执行时的流程简单化。

12. php isset与unset区别与使用方法

很多新手都会把issetunset给搞混,其实它们二个的区别很大,可以说不存在什么关系哦,我想可能是长得像吧,所以才这样认为哦,那么下面我们就来看看isset的语法吧.

isset(变量);

说明:

isset是用来判断变量存在不,如果是就返回true否则就返回false,这个变量对我们很有用,我们可以用来判断如get post等变量是否在哦.

isset实例"

<?php

$a = "test";

$b = "anothertest";

echo isset($a)? 'true' : 'false';

?>

输出结果为:true

下面我们再来看看unset实例吧.

<?php

$a = "test";

$b = "anothertest";

unset($a);

echo isset($a)? 'true' : 'false';

?>

输出就为false;

知道为什么吗?因为unset是销毁变量哦.

From: http://www.111cn.net/phper/18/f0616173780bc96feabea7a10ea7d9ae.htm

顶一下
(0)
踩一下
(0)