php dirname __FILE__
dirname(__FILE__) 就是取得当前文件所在的目录,是返回真实的文件所在路径下面来看几个实例
*/
//直接__FILE__看实例
$url = __FILE__;
echo $url;
//结果为
//D:wwwfkt.php
//再来看看__FILE__配合dirname使用
$url = dirname(__FILE__);
echo $url;
//结果
//D:wwwfk
//看个其它的realpath
echo realpath($url);
//结果
//D:wwwfk
die();
isset和is_null的功能几乎完全相反的一样..
是不是isset就是一个is_null的相反的别名?
诶, 要说区别, 那还真的是很多~
一切的不同都是因为: is_null是函数, isset是语句.
isset是语句, 和echo, print一样, 是PHP本身的一种语言结构.
而is_null是函数, 和我们一般的函数一样, 可以做为可变函数调用.
你也许会说, 好了,好了,我知道函数和语句的区别, 但到底是TMD什么区别?
诶, 所谓语句,语言结构, 就是说, 是语言本身支持的语句, 标识符.
比如, for, foreach, continue 等等, 它们在语法分析的时刻就被”抹掉”(逻辑上替代了)了.
让我们看看isset这个语句在语法分析的过程中, 是如何被”抹掉”的.
1. 首先, 在词法分析的时候, isset会被识别为T_ISSET标识符.2. 而在语法分析阶段, isset($var)这个指令, 会被分析成一条Opcode:ZEND_ISSET_ISEMPTY_VARS.
你可以理解isset就想C语言里面的宏, 在编译/执行之前已经被展开了.
因为这个, 所以在表现上, 会有如下的不同:
因为is_null是函数, 所以它可以通过如下方式调用:
<?php$var = NULL;$func = "is_null";$func($var);?>
而, isset因为是语句, 所以不能这样调用.
因为is_null是函数, 所以它可以接受函数返回值做为参数, 而isset不行(当然, 如果PHP想支持, 其实也是可以的, 只不过就要增加编译阶段的复杂度了):
<?phpis_null(intval("0x45"));//OKisset(intval("0x45"));//PHP Fatal error: Can't use function return value in write contextis_null(NULL);//OKisset(NULL);//PHP Parse error: syntax error?>
说了这么多isset的缺点了, 说点它的优点吧:
因为isset是语句, 所以它快!
在一千万次的简单检测语句循环中, 对比结果如下:
<?php$a="laruence":isset($a); //用时: 1.15165400505sis_null($a);//用时: 3.78501200676s?>
因为isset叫做isset, 所以它在检测未定义变量的时候, 不会产生NOTICE:
<?phpisset($laruence);//OKis_null($laruence);//PHP Notice: Undefined variable: laruence?>
那么, 对于什么时候用isset什么时候用is_null, 我有什么建议呢?
诶, 我的建议是, 用函数做函数应该做的事情~, 听起来象废话?
isset => is set? => 变量有没有被赋值(声明)
is_null => is null? => 变量为空么?
另外, 如果要用is_null, 我建议使用 “=== NULL” 来代替, 它不仅语义和is_null一致, 结果一致, 速度还和isset差不多:
在一千万次的简单检测语句循环中, 对比结果如下:
<?php$a="laruence":isset($a); //用时: 1.15165400505sis_null($a);//用时: 3.78501200676s$a===NULL; //用时: 1.21655392647s?>
php 删除cookie有效方法
关于删除cookie的说明开始-----
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )
要删除cookie需要确保它的失效期是在过去,才能触发浏览器的删除机制。
下面的例子说明了如何删除刚才设置的cookie:
<?php
//将过期时间设为一小时前
setcookie("TestCookie", "", time() - 3600);
setcookie("TestCookie", "", time() - 3600, "/~rasmus/", ".utoronto.ca", 1);
?>
-----关于删除cookie的说明结束-----
删除一个cookie的方法就是把这个cookie的有效期设置为当前时间以前,这也是几乎所有php程序员都会这么做。
后来一个初接触php的朋友告诉我,他在程序中本想把一个cookie的值设置为空,结果导致这个cookie直接被删除。我当时的第一反应是不相信,于是测试
了一下:
setcookie("testcookie", '');
print_r($_COOKIE);
结果果然是整个$_COOKIE数组都是空的,而非仅仅$_COOKIE['testcookie']为空。于是用winsock抓包,观察返回的http头,发现http头竟然是“Set-Cookie: testcookie=deleted; expires=Mon, 18-Jun-2007 02:42:33 GMT”,这说明“setcookie("testcookie", '');”的的确确是将testcookie这个cookie直接删除,而关于这种情况在php手册中完全没有说明。
最后阅读php源码,终于发现真相(这就是开源的好处了,有什么不清楚的内幕,直接查源码)。
以下代码可以在php5.20的linux源码包中ext/standard/head.c第99行附近找到:
if (value && value_len == 0) {
/*
* MSIE doesn't delete a cookie when you set it to a null value
* so in order to force cookies to be deleted, even on MSIE, we
* pick an expiry date 1 year and 1 second in the past
*/
time_t t = time(NULL) - 31536001;
dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, t, 0 TSRMLS_CC);
sprintf(cookie, "Set-Cookie: %s=deleted; expires=%s", name, dt);
efree(dt);
} else {
sprintf(cookie, "Set-Cookie: %s=%s", name, value ? encoded_value : "");
if (expires > 0) {
strcat(cookie, "; expires=");
dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, expires, 0 TSRMLS_CC);
strcat(cookie, dt);
efree(dt);
}
}
源码中清清楚楚的显示“if (value && value_len == 0)”,当“value_len”为0时,“sprintf(cookie, "Set-Cookie: %s=deleted; expires=%s", name, dt);”会发送删除cookie的http头给浏览器。
最后我们可以得出结论:在php中使用“setcookie($cookiename, '');”或者“setcookie($cookiename, NULL);”都会删除cookie,当然这些手册中并没有。
is_array -- 检测变量是否是数组
描述
bool is_array ( mixed var)
如果 var 是 array,则返回 TRUE,否则返回 FALSE。
is_array($array)
判断一个值是否为数组
*/
$array = array('','');
if( is_array( $array ) )
{
echo 'is array';
}
// 输出结果 is array
$str ='';
if( is_array( $str ) )
{
echo 'is array';
}
else
{
echo 'is string';
}
//输出结果为is string
php文件上传经典代码
function FileUpload( $resourceType, $currentFolder, $sCommand )
{
if (!isset($_FILES)) {
global $_FILES;
}
$sErrorNumber = '0' ;
$sFileName = '' ;
if ( isset( $_FILES['NewFile'] ) && !is_null( $_FILES['NewFile']['tmp_name'] ) )
{
global $Config ;
$oFile = $_FILES['NewFile'] ;
// Map the virtual path to the local server path.
$sServerDir = ServerMapFolder( $resourceType, $currentFolder, $sCommand ) ;
// Get the uploaded file name.
$sFileName = $oFile['name'] ;
$sFileName = SanitizeFileName( $sFileName ) ;
$sOriginalFileName = $sFileName ;
// Get the extension.
$sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ;
$sExtension = strtolower( $sExtension ) ;
if ( isset( $Config['SecureImageUploads'] ) )
{
if ( ( $isImageValid = IsImageValid( $oFile['tmp_name'], $sExtension ) ) === false )
{
$sErrorNumber = '202' ;
}
}
if ( isset( $Config['HtmlExtensions'] ) )
{
if ( !IsHtmlExtension( $sExtension, $Config['HtmlExtensions'] ) &&
( $detectHtml = DetectHtml( $oFile['tmp_name'] ) ) === true )
{
$sErrorNumber = '202' ;
}
}
// Check if it is an allowed extension.
if ( !$sErrorNumber && IsAllowedExt( $sExtension, $resourceType ) )
{
$iCounter = 0 ;
while ( true )
{
$sFilePath = $sServerDir . $sFileName ;
if ( is_file( $sFilePath ) )
{
$iCounter++ ;
$sFileName = RemoveExtension( $sOriginalFileName ) . '(' . $iCounter . ').' . $sExtension ;
$sErrorNumber = '201' ;
}
else
{
move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;
if ( is_file( $sFilePath ) )
{
if ( isset( $Config['ChmodOnUpload'] ) && !$Config['ChmodOnUpload'] )
{
break ;
}
$permissions = 0777;
if ( isset( $Config['ChmodOnUpload'] ) && $Config['ChmodOnUpload'] )
{
$permissions = $Config['ChmodOnUpload'] ;
}
$oldumask = umask(0) ;
chmod( $sFilePath, $permissions ) ;
umask( $oldumask ) ;
}
break ;
}
}
if ( file_exists( $sFilePath ) )
{
//previous checks failed, try once again
if ( isset( $isImageValid ) && $isImageValid === -1 && IsImageValid( $sFilePath, $sExtension ) === false )
{
@unlink( $sFilePath ) ;
$sErrorNumber = '202' ;
}
else if ( isset( $detectHtml ) && $detectHtml === -1 && DetectHtml( $sFilePath ) === true )
{
@unlink( $sFilePath ) ;
$sErrorNumber = '202' ;
}
}
}
else
$sErrorNumber = '202' ;
}
else
$sErrorNumber = '202' ;
$sFileUrl = CombinePaths( GetResourceTypePath( $resourceType, $sCommand ) , $currentFolder ) ;
$sFileUrl = CombinePaths( $sFileUrl, $sFileName ) ;
SendUploadResults( $sErrorNumber, $sFileUrl, $sFileName ) ;
exit ;