20090922_php学习笔记
1. 单引号和双引号区别http://cuckoosnest.iteye.com/blog/4738882. define常量,常量引用不要加$
set_include_path设置include目录
<?php
// Works as of PHP 4.3.0
set_include_path('/inc');
// Works in all PHP versions
ini_set('include_path', '/inc');
?>
<?php
$path = '/usr/lib/pear';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
// In this example we add /usr/lib/pear to the end of the existing include_path.
?>
3. 路径相关:dirname( __FILE ) 当前文件所在目录,被include的文件中调用此方法,也是指被include的文件所在目录。
3. 字符串函数
str_replace("\\", '/', substr(dirname(__FILE__), 0, -7));//这里的 "\\" 不能写成 '\'
substr()方法
<?php
echo substr('abcdef', 1); // bcdef
echo substr('abcdef', 1, 3);// bcd
echo substr('abcdef', 0, 4);// abcd
echo substr('abcdef', 0, 8);// abcdef
echo substr('abcdef', -1, 1); // f
// Accessing single characters in a string
// can also be achived using "curly braces"
$string = 'abcdef';
echo $string{0}; // a
echo $string{3}; // d
echo $string{strlen($string)-1}; // f
?>
string implode ( string glue, array pieces ) 相当于list join
<?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
?>
string stripslashes ( string str )
<?php
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
// Example
$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);
// Output
print_r($array);
?>
//打印出
Array
(
=> f'oo
=> b'ar
=> Array
(
=> fo'o
=> b'ar
)
)
引用
The strcasecmp() function compares two strings.
strcasecmp()函数的作用是:对两个字符串进行比较。
This function returns:
该函数将返回下列值:
0 - if the two strings are equal
0 – 如果字符串相等
<0 - if string1 is less than string2
<0 – 如果string1小于string2
int strpos ( string haystack, mixed needle [, int offset] ) 查找needle在haystack中的位置,
注意:如果不匹配到needle,则返回false,也有可能返回0,因此判断这个函数返回值必须用 === 符号!
4. 时间函数 microtime()
对脚本进行计时
<?php
/**
* Simple function to replicate PHP 5 behaviour
*/
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
// Sleep for a while
usleep(100);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";
?>
int time ( void ) 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。
5. php定义变量: var 一般是出现在类对象中,一般的过程和函数不需要 var定义变量。
6. magic_quotes_gpc magic_quotes_runtime 配置的作用: http://cuckoosnest.iteye.com/blog/473950
7. 关于全局变量 超全局变量,获取用户post get参数,获取服务器信息
引用
从 PHP 4.1.0 开始,PHP 提供了一套附加的预定数组,这些数组变量包含了来自 web 服务器(如果可用),运行环境,和用户输入的数据。这些数组非常特别,它们在全局范围内自动生效,例如,在任何范围内自动生效。因此通常被称为自动全局变量(autoglobals)或者超全局变量(superglobals)。(PHP 中没有用户自定义超全局变量的机制。)超全局变量罗列于下文中;但是为了得到它们的内容和关于 PHP 预定义变量的进一步的讨论以及它们的本质,请参阅预定义变量。而且,你也将注意到旧的预定义数组($HTTP_*_VARS)仍旧存在。自 PHP 5.0.0 起,长格式的 PHP 预定义变量可以通过设置 register_long_arrays 来屏蔽。
引用
PHP 4.2.0 以及后续版本中,PHP 指令 register_globals 的默认值为 off。这是 PHP 的一个主要变化。让 register_globals 的值为 off 将影响到预定义变量集在全局范围内的有效性。例如,为了得到 DOCUMENT_ROOT 的值,将必须使用 $_SERVER['DOCUMENT_ROOT'] 代替 $DOCUMENT_ROOT,又如,使用 $_GET['id'] 来代替 $id 从 URL http://www.example.com/test.php?id=3 中获取 id 值,亦或使用 $_ENV['HOME'] 来代替 $HOME 获取环境变量 HOME 的值。
8. void session_set_cookie_params ( int lifetime [, string path [, string domain [, bool secure]]] )
session_set_cookie_params方法修改cookie参数,覆盖php.ini中的定义,作用域是页面,因此每次请求都应该在session_start()调用之前调用此方法。
9. int extract ( array var_array [, int extract_type [, string prefix]] )把map型数组定义的名值对extract成变量。
这个方法在magic_quotes_gpc配置为false的时候需要调用:
$_POST = new_addslashes($_POST);
$_GET = new_addslashes($_GET);
$_COOKIE = new_addslashes($_COOKIE);
@extract($_POST);
@extract($_GET);
@extract($_COOKIE);
10. string mysql_real_escape_string ( string unescaped_string [, resource link_identifier] )
本函数将 unescaped_string 中的特殊字符转义,并计及连接的当前字符集,因此可以安全用于 mysql_query()。
注: mysql_real_escape_string() 并不转义 % 和 _。
11.
页:
[1]