系统程序执行

User Contributed Notes

ohcc at 163 dot com 29-Nov-2015 05:34
You can use a pair of backquotes `` to run system commands.

<?php
    header
('Content-Type: text/html; charset=gbk');
    echo `
ping -n 1 www.51-n.com`;// on windows only
?>

it returns the whole output of the command called as a string.

Variables, class properties and return values of class methods   between two ` signs will be replaced with their values.

<?php
    header
('Content-Type: text/html; charset=gbk');
   
$domain = 'www.51-n.com';
    echo `
ping -n 1 {$domain}`;
?>

<?php
    header
('Content-Type: text/html; charset=gbk');
   
    class
Ping {
       
        private
$domain;
        private
$count;
       
        public function
setDomain($domain){
           
$this->domain = $domain;
        }
       
        public function
setCount($count){
           
$this->count = $count;
        }

        public function
getDomain(){
            return
$this->domain;
        }

        public function
ping(){
            return `
ping -n {$this->count} {$this->getDomain()}`;
        }

    }
   
   
$p = new Ping;
   
$p->setDomain('www.wuxiancheng.cn');
   
$p->setCount(1);
    echo
$p->ping();
?>