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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

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.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment