您的位置:首页 > 编程语言 > PHP开发

PHP手册笔记

2015-11-22 11:24 495 查看
<?php
getenv — 获取一个环境变量的值
$ip  =  getenv ( 'REMOTE_ADDR' );
// 或简单仅使用全局变量($_SERVER 或 $_ENV)
$ip  =  $_SERVER [ 'REMOTE_ADDR' ];

getopt — 从命令行参数列表中获取选项

当使用命令行执行时:php script.php arg1 arg2 arg3
var_dump ( $argv );
//结果为
array(4) {
[0]=>  string(10) "script.php"
[1]=>   string(4) "arg1"
[2]=>   string(4) "arg2"
[3]=>  string(4) "arg3"
}
当使用命令行执行时:php script.php arg1 arg2 arg3
var_dump ( $argc ); //结果为: int(4)

$GLOBALS — 引用全局作用域中可用的全部变量
function  test () {
$foo  =  "local variable" ;
echo  '$GLOBALS找全局变量所以找到Example content: '  .  $GLOBALS [ "foo" ] .  "\n" ;
echo  '正常的找局部 local variable: '  .  $foo  .  "\n" ;
}
$foo  =  "Example content" ;
test ();

迭代(目的实例化一个对象后可以遍历那个对象) 写类时我们要继承Iterator的话就必须多些5个方法 1.current() 2.key() 3.next() 4.rewind() 5.valid
聚合式迭代器(目的实例化一个对象后可以遍历那个对象)   我们写类时要是继承 IteratorAggregate  就必须多写一个固定死的方法 getIterator(){ return new ArrayIterator($this); }
数组式访问 (目的实例化一个对象后可以遍历那个对象)  我们写类时要是继承ArrayAccess()接口 就必须多写4个方法
序列化接口  多写2个方法

套接字上下文选项 — 套接字上下文选项列表
//   '7000'
$opts  = array(
'socket'  => array(
'bindto'  =>  '0:7000' ,
),
);
$context  =  stream_context_create ( $opts );
echo  file_get_contents ( 'http://www.example.com' ,  false ,  $context );

HTTP context 的选项列表
$opts  = array( 'http'  =>
array(
'method'   =>  'POST' ,
'header'   =>  'Content-type: application/x-www-form-urlencoded' ,
'content'  =>  $postdata
)
);
$context  =  stream_context_create ( $opts );
$result  =  file_get_contents ( 'http://example.com/submit.php' ,  false ,  $context );

FTP 上下文选项列表
$stream_options = array('ftp' => array('overwrite' => true));
$stream_context = stream_context_create($stream_options);

CURL 上下文选项列表
$opts  = array( 'http'  =>
array(
'method'   =>  'POST' ,
'header'   =>  'Content-type: application/x-www-form-urlencoded' ,
'content'  =>  $postdata
)
);
$context  =  stream_context_create ( $opts );
$result  =  file_get_contents ( 'http://example.com/submit.php' ,  false ,  $context );

加载扩展 如果gd扩展已加载,返回 TRUE ,否则返回 FALSE 。
if (! extension_loaded ( 'gd' )) {
if (! dl ( 'gd.so' )) {
exit;
}
}

// 引入一个扩展的例子,基于操作系统
if (! extension_loaded ( 'sqlite' )) {
if ( strtoupper ( substr ( PHP_OS ,  0 ,  3 )) ===  'WIN' ) { //判断操作系统
dl ( 'php_sqlite.dll' );
} else {
dl ( 'sqlite.so' );
}
}

获取当前 PHP 脚本所有者名称   echo  'Current script owner: '  .  get_current_user ();

— 返回所有常量的关联数组,键是常量名,值是常量值
define ( "MY_CONSTANT" ,  1 ); print_r ( get_defined_constants ( true ));

输入模块名称 返回模块所以方法  print_r ( get_extension_funcs ( "xml" ));

查看php 引入了哪些文件 print_r(get_included_files());

返回所有编译并加载的模块   print_r ( get_loaded_extensions ());

// 如果启用了魔术引号
echo  $_POST [ 'lastname' ];              // O\'reilly  //增加反斜线
echo  addslashes ( $_POST [ 'lastname' ]);  // O\\\'reilly
// 适用各个 PHP 版本的用法
if ( get_magic_quotes_gpc ()) {//为关闭时返回 0,否则返回 1
$lastname  =  stripslashes ( $_POST [ 'lastname' ]); //去掉反斜线
}else {
$lastname  =  $_POST [ 'lastname' ];
}

// 如果使用 MySQL
$lastname  =  mysql_real_escape_string ( $lastname );

echo  $lastname ;  // O\'reilly
$sql  =  "INSERT INTO lastnames (lastname) VALUES (' $lastname ')" ;

getmyuid — 获取 PHP 脚本所有者的 UID

获取当前 PHP 脚本拥有者的用户组 ID。  getmygid

getmyinode — 获取当前脚本的索引节点(inode)

getmypid — 获取 PHP 进程的 ID

ini_get_all — 获取所有配置选项

ini_get — 获取一个配置选项的值

memory_get_peak_usage — 返回分配给 PHP 内存的峰值

memory_get_usage — 返回分配给 PHP 的内存量

php.ini文件的位置 echo php_ini_loaded_file ();

返回 web 服务器和 PHP 之间的接口类型  PHP 常量 PHP_SAPI 具有和 php_sapi_name() 相同的值。

打印 PHP 贡献者名单  phpcredits ( CREDITS_ALL  -  CREDITS_FULLPAGE );
phpversion — 获取当前的PHP版本
set_time_limit — 设置脚本最大执行时间

sys_get_temp_dir — 返回用于临时文件的目录

对比两个「PHP 规范化」的版本数字字符串
if ( version_compare ( PHP_VERSION ,  '5.3.0' ) >=  0 ) {
echo  'I am at least PHP version 5.3.0, my version: '  .  PHP_VERSION  .  "\n" ;
}

zend_thread_id — 返回当前线程的唯一识别符

zend_version — 获取当前 Zend 引擎的版本

array_combine($array(一维数组的值做键), 另一个一维数组的值做值);新数组创建成功
array_column($array, '多维数组中的某元素值做值', '某元素值做键');
str_repeat ( "-=" ,  10 ); //-=-=-=-=-=-=-=-=-=-=

echo nl2br ( "foo isn't\n bar" );//foo isn't<br />bar

echo lcfirst ('HelloWorld' );   // helloWorld

// Outputs: A 'quote' is <b>bold</b>
echo  $a = htmlentities ( $str ,  ENT_QUOTES );//引号也被转换
echo  html_entity_decode ( $a );//引号不被转换

// 注意,这里的引号不会被转换
echo  htmlspecialchars_decode ( $str ,  ENT_NOQUOTES );
echo htmlspecialchars ( "<a href='test'>Test</a>" ,  ENT_QUOTES ); // <a href='test'>Test</a>

// count_chars ( string $string [, int $mode = 0 ] )
echo $str =convert_uuencode ( 'string' );//1个参数(string) 目的对string进行编码 返回结果

echo  convert_uudecode ( $str );//解码一个 uuencode 编码的字符串

$str = chunk_split('abcdefghi', 2, '=');//3个参数,目的用指定字符串将其分割为一定长度,返回分割后的字符。

stripslashes — 反引用一个引用字符串
addcslashes(string, 'A-z')//两个参数 目的转义 字符串  返回转以后的字符串
addslashes(string);//一个参数  目的转义4个特殊符号 ‘ “ \ null 返回转以后的字符串
string stripcslashes ( string $str ) 反引用一个使用 addcslashes() 转义的字符串

bin2hex(二进制) //一个参数  目的2进制转16进制 返回16进制
hex2bin — 转换十六进制字符串为二进制字符串

chop() rtrim() 的别名  //2个参数,目的去除右边空白或字符  返回去除后的结果
/**/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: