PHP笔记整理——基础语法

背景

初步了解php语言的语法之后,将一些常用的写法和规则记录下来。php与java语法类似的部分,这里就不做过多说明。

简介

服务器脚本语言;
跨平台;
快捷高效。

基本语法

php文件使用以下标签,文件扩展名为“.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)
{
  //todo
}

参数带默认值

function test($disable = TRUE)
{
    //todo
}

3、数组

$list1=array("a","b","c");
$list2 = array();

数组的相关操作:

//给数组指定index元素赋值
$list[0]="a";
//获取长度
$arrlength= count($list);

//遍历数组
for($x=0;$x<$arrlength;$x++) {
  echo $list[$x];
  echo "<br>";
}

//isset 某元素是否在数组中
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();
	//select
	$num = $db->query_result_single("select value from table where `key`='{$key}'", 0);
	// insert
	//$sql = "insert into " . $tab . " (" . implode(',', $keys) . ") values " . $data . ' ' . $update;
	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;
    }

未完待续……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值