search
Homephp教程php手册php静态变量define,static,const

php静态变量define,static,const

Jun 13, 2016 am 11:18 AM
constdefinephpstaticScopeInsidefunctionvariablestatic

 

静态变量只存在于函数作用域内,也就是说,静态变量只存活在栈中。一般的函数内变量在函数结束后会释放,比如局部变量,但是静态变量却不会。就是说,下次再调用这个函数的时候,该变量的值会保留下来

最简单定义方法

define() 函数定义一个常量。

常量类似变量,不同之处在于:

在设定以后,常量的值无法更改
常量名不需要开头的美元符号 ($)
作用域不影响对常量的访问
常量值只能是字符串或数字
语法

define(name,value,case_insensitive)

class foo {
   static $my_static = 5;
   public $my_prop = 'bla';
}

print foo::$my_static; copyright dedecms
$obj = new foo;
print $obj->my_prop;
?>

const 是常量,即从程序运算开始到结束,数值不变的量。

class say_const{  
const charset=”中国”;  
publice function say_hello(){  
echo slef::charset;  
}  
}  
$const1=new say_const()’  
$const1->say_hello();  
?>  

输出就是“中国”

下面一款完整的静态变量

function write_file($file,$msg,$usecheck=5){
 static $check=1;//这里用到了~
 $f=@fopen($file,"a+b");
 if($f){
  if(flock($f,lock_ex)){
   fwrite($f,$msg);
   flock($f,lock_un);
   fclose($f);
  }else{
   fclose($f);
   $check++;
   if($check     sleep(1);//暂停下程序,等其他进程释放该资源
    write_file($file,$msg);
   }
  }
 }else{
  $check++;
  if($check    sleep(1);//暂停下程序,等其他进程释放该资源
   write_file($file,$msg);
  }
 }
}


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.