判断php运行环境的函数php_sapi_name()
判断php运行环境的函数php_sapi_name()
我们一般情况下,都是在apache下面运行我们的php程序,当然也有些人是用IIS环境的
我们要是想知道我们目前运行的环境是什么的话,那我们可以用函数php_sapi_name()来测试
代码:
1.<?php2.echo php_sapi_name();在apache环境下面输出的结果是“apache2handler”;
在cgi模式下输出的结果是“cgi-fcgi”
要是在命令行模式下面运行的话,那么输出的结果是:”cli”
依据这个内容我们可以判断当前运行的环境是什么!
那么在命令行下怎么运行呢?
如下:
进入DOS 进入php.exe文件的地址 如我的是:d:/wamp/bin/php/php5.3.3/
然后输入php.exe “文件的绝对路径” 如:>php.exe d:/wamp/www/info.php
既可以了。
01.<?php02.if(substr(PHP_SAPI_NAME(),0,3) !== 'cli'){03.die("该程序只能在CLI模式下运行!");04.}05.//print_r(get_defined_constants());06. 07.Class Test{08.public $name = "";09.public $value = "";10.public $content = "";11.public $arr = array();12. 13.public function __construct(){14.$this->top();15.}16. 17.public function top(){18.fwrite(STDOUT,"请输入参数和值:");19.$this->content = trim(fgets(STDIN));20.preg_replace('/\s+/',' ',$this->content);21.$this->arr = explode('->',$this->content);22.$this->name = $this->arr['0'];23.$this->value = $this->arr['1'];24.}25. 26.public function ouput(){27.echo"=========================================28.| THE NAME  |  .$this->name29.=========================================30.| THE VALUE |  '.$this->value;31.=========================================\n";32.}33. 34.}35.while(true){36.$t = new Test();37.$t->ouput();38.} 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号