| 背景初步了解php语言的语法之后,将一些常用的写法和规则记录下来。php与java语法类似的部分,这里就不做过多说明。 简介服务器脚本语言;跨平台;
 快捷高效。
 基本语法php文件使用以下标签,文件扩展名为“.php” <?php
?>
 1、变量$变量以$开头,大小写敏感。     $totalPage = 0;
	$title = "abc";
 global在函数内访问全局变量 $bool_test = false;
function method()
{
    global $bool_test;
    $bool_test = true;
}
 static函数执行后,想要保留某个变量的值,可以用static。可修饰方法和变量。
 	 static $status;
    public static function test(){
    }
 echo输出一个以上字符串。print:只能输出一个字符串
 	 echo "abc";
	 print "abc";
 define定义常量 define('STATUS', 0);
 2、函数带参数的函数 function test( $message, $filepath)
{
  
}
 参数带默认值 function test($disable = TRUE)
{
    
}
 3、数组$list1=array("a","b","c");
$list2 = array();
 数组的相关操作: 
$list[0]="a";
$arrlength= count($list);
for($x=0;$x<$arrlength;$x++) {
  echo $list[$x];
  echo "<br>";
}
isset($list1[$a])
 $argv 传递给脚本的参数数组,第一个参数总是当前脚本的文件名,因此 $argv[0] 就是脚本文件名。 4、对象符号“->”标识对象执行方法或取得属性。如下所示。可以理解成 this.color=color。
 <?php
class Car
{
  var $color;
  function Car($color="green") {
    $this->color = $color;
  }
  function what_color() {
    return $this->color;
  }
}
?>
 5、引用require内置函数,引入或包含外部的php文件。 require_once() 与require()作用相同,但如果指定的文件如果已经被包含过,则不会再次包含。它可以避免函数重定义,变量重新赋值等问题。 require("test.php");
define('VALUE', 'test');
require_once(VALUE . '/test.php');
 6、数据库操作
function executeTest(){
	$db =getDb();
	
	$num = $db->query_result_single("select value from table where `key`='{$key}'", 0);
	
	
	return $num;
}
function getDb()
{
    global $db_config;
    $mysql = new MySQLdb($db_config['host'], $db_config['user'], $db_config['pwd'], false, $db_config['db_name']);
    return $mysql;
}
function query_result_single($string, $def = false)
{
    $params = func_get_args();
    $result = call_user_func_array(array($this, 'query_result'), $params);
    if (!$result) return $def;
    return array_shift($result);
}
 7、文件操作function test($tube, $return = false)
{
    $file_dir = '/test';
    if (!is_dir($file_dir)) {
        mkdir($file_dir);
        chmod($file_dir, 0777);
    }
    $lock_pid = fopen("$file_dir/$tube.pid", 'a+');
    if ($lock_pid) {
        if (flock($lock_pid, LOCK_EX | LOCK_NB)) {
            if (!$return) {
                fclose($lock_pid);
                return true;
            }
            return $lock_pid;
        }
        fclose($lock_pid);
    }
    return false;
}
 mkdir():创建目录chmod():赋权限
 fopen():打开文件
 fclose():关闭文件
 fwrite():写入文件
 is_dir():指定文件是否是目录
 8、网络请求 static public function sendParamsByPost($url, $paramsArray)
    {
        $postdata = http_build_query($paramsArray);
        $length = strlen($postdata);
        $cl = curl_init($url);
        curl_setopt($cl, CURLOPT_POST, true);
        curl_setopt($cl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($cl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-length: " . $length));
        curl_setopt($cl, CURLOPT_TIMEOUT, 20);  
        curl_setopt($cl, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($cl, CURLOPT_RETURNTRANSFER, true);
        $content = curl_exec($cl);
        if (curl_errno($cl)) 
        {
            throw new Exception(curl_error($cl), 0);
        } else {
            $httpStatusCode = curl_getinfo($cl, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                throw new Exception($reponse, $httpStatusCode);
            }
        }
        curl_close($cl);
        $content = html_entity_decode($content);
        return $content;
    }
 未完待续…… |