正确源自错误, 失败是成功她妈, 先来个错误的: i=100; echo “xxx”+i;

语法

  1. php声明变量需要$开头, 常量不需要. 因此:

    $i=100;
    define("FOO",     "something"); // 这是一个正常的常量, 不在类范围内的.
    #变量有3个关键函数
    unset($i); # 释放这个变量
    isset($i); # 检查这个变量是否设置了
    empty($i); # 检查这个变量是否为空
    
  2. php的字符串链接用点 . php的字符串如果是双引号, 则解析其中的变量.:

    echo "xxx".i //如果这样写这个i是一个常数才行.
    echo "xxx".$i //这样写就对了. 这样就是一个变量了.
    echo "xxx $i" //这个写法才是好的, 合理的写法.
    

函数内容

参考: 2016-08-25-php函数和他的参数.md

这货竟然是functional的.

  1. 可变变量.

    $hello='hello world';
    $b='hello';
    echo $$b; // 此处显示hello world.
    ${$a[1]}; // 数组有歧义, 因此必须大括号使用.
    ${$a}[1];
    

OO

  1. 水平组合

    trait ezcReflectionReturnInfo {
        function getReturnType() { /*1*/ }
        function getReturnDescription() { /*2*/ }
    }
    
    class ezcReflectionMethod extends ReflectionMethod {
        use ezcReflectionReturnInfo;
        /* ... */
    }
    
    class ezcReflectionFunction extends ReflectionFunction {
        use ezcReflectionReturnInfo;
        /* ... */
    }
    
  2. 静态和常量 const和static, ->::

    // 在类的成员方法里面,可以用 ->(对象运算符):$this->property(其中 property 是该属性名)这种方式来访问非静态属性。静态属性则是用 ::(双冒号):self::$property 来访问。
    define("FOO",     "something"); // 这是一个正常的常量, 不在类范围内的.
    class MyClass
    {
        const constant = 'constant value'; // 这个是常量, 注意它并没有使用$声明
      public static $my_static = 'foo'; // 这个是静态的
    
        function showConstant() {
            echo  self::constant . "\n";
        }
          public static function aStaticMethod() {
            // ...
        }
    
    }
    print MyClass::$my_static . "\n"; // 这个是访问静态属性的方法, 用类名直接访问.
    MyClass::aStaticMethod(); // 访问静态方法
    echo MyClass::constant . "\n"; // 常量
    $classname = "MyClass";
    echo $classname::constant . "\n"; // 自 5.3.0 起, 常量访问.
    $classname::aStaticMethod(); // 自 PHP 5.3.0 起, 静态方法.
    
    $class = new MyClass();
    $class->showConstant(); // 正常的类方法.
    echo $class::constant."\n"; // 自 PHP 5.3.0 起, 常量.
    

系统函数连数据啥的

<?php
//http://www.lampbrother.net
//兄弟连论坛
    $link = mysql_connect('localhost','root','123456')or die("连接数据库失败!");//连接数据库
    mysql_select_db('shoujiguishu')or die("选择数据库失败!");//选择数据库
    mysql_set_charset('utf8');//设置字符集编码
    $keyword = $_POST['keyword'];//得到用户提交的号码
    $num = substr($keyword,0,7);//把号码截取成7位和数据库匹配
    $sql = "SELECT num,quhao,city,type FROM shouji WHERE num = {$num}";//准备查询语句
    $res = mysql_query($sql);//发送语句
    if($res && mysql_num_rows($res)){//判断如果执行成功并且得到的结果数不为0
        $card = mysql_fetch_assoc($res);//把结果集给变量$card 一个索引数组
        $istrue = 1;//设置一个标志,用来判断是否查询成功
    }else{
        $msg = "请输入正确的手机号码!";//不成功,则返回一个提示信息
        $istrue = 0;//不成功,标志为0
    }
    
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>Index</title>
        <style>
            *{
                margin:0;
                padding:0;
            }
            .main{
                width:540px;
                margin:0 auto;
            }
            .top{
                width:540px;
                height:63px;
                background:url('./img/tit.png');
            }
            .tit{
                color:#026EA3;
http://www.lampbrother.net                font:normal bold 16px/63px serif;
                text-indent:10px;
            }
            .search{
                width:540px;
                
            }
            .search_text{
                width:260px;
                height:31px;
                margin-left:50px;
                margin-top:30px;
            }
            .bun_sub{
                width:80px;
                height:31px;
                background:url(./img/submit.gif);
                border:0;
            }
            .bun_res{
                width:80px;
                height:31px;
                background:url('./img/reset.gif');
                border:0;

            }
            .bun{
                width:170px;
                float:left;
                line-height:75px;
            }
            .sear{
                width:350px;
                float:left;
            }
            .content{
                float:left;
                width:538px;
                border-left:1px solid #A9C9D0;
                border-right:1px solid #A9C9D0;
                border-bottom:1px solid #A9C9D0;
            }
            .result{
                width:500px;
                margin:0 auto;
            }
            p{
                text-indent:30px;
                font:normal normal 16px/35px serif;
                color:#026EA3;
            }
        </style>
    </head>
    <body>
        <div class="main">
            <div class="top">
                <div class="tit">
                    手机归属地查询
                </div>
            </div>
            <div class="content">
                <div class="search">
                    <form action="" method="post">
                        <div class="sear"><input type="text" class="search_text" name="keyword" /></div>
                        <div class="bun"><input type="submit" class="bun_sub" name="sub" value="" />
                        <input type="reset" class="bun_res" name="reset" value="" /></div>
                    </form>    
                </div>
                <div class="result">
                <?php
                    if($istrue == 1){//判断标志为1时,说明查询成功,则执行下面代码
                ?>
                    <p>你查询的手机号码为:<?php echo $keyword ?></p>
                    <p>所属的诚实为:<?php echo $card['city'] ?></p>
                    <p>城市的区号为:<?php echo $card['quhao'] ?></p>
                    <p>卡的类型为:<?php echo $card['type'] ?></p>
                <?php
                    }else{//否则查询不到则执行下面代码
                        echo "<p>$msg</p>";
                    }
                ?>
                </div>
            </div>
        </div>
    </body>
</html>

php里面空的object如何判断

### 样例
{"messages":{},"request_id":"qm2unbuef74e"}
stdClass Object ( 
	[messages] => stdClass Object ( ) 
	[request_id] => qm2unbuef74e ) 
#这个message的空, 可以用属性empty判断, 也可以用后面的数组方法判断.
 if(empty($resp->messages->tmc_message)){...}

对象变数组挺有用的

if (!(array)$resp->messages) echo '<br>hello 2.05  empty<br>'; //这样就可以判断属性为空的情况了了.

对象和数组

  • To access an array member you use $array[‘KEY’];

  • To access an object member you use $obj->KEY;

  • To access an object member inside an array of objects:

    • $array[0] // Get the first object in the array
    • $array[0]->KEY // then access its key
  • You may also loop over an array of objects like so:

    foreach ($arrayOfObjs as $key => $object) {
    	echo $object->object_property;
    }
    

sdk

  1. 是否使用sdk, 我也不知道, 可以试试phpstorm
  2. 注册时选择License server,填http://idea.lanyus.com,然后点击OK,就注册了
  3. 此方法是在IDEA15评论中看到的,试了一下PHPSTORM10,一样可用。

官网

php官网: http://php.net/manual/zh/internals2.memory.php

注册参考: http://www.oschina.net/news/67681/phpstorm-10

curl

<?php 
  // 把主页导出到文件.
$ch = curl_init("http://www.example.com/"); # 初始化
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp); # 设置参数
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch); # 执行
curl_close($ch); # 关闭
fclose($fp);
?>

白屏为了啥

  • 一般是语法错误.
  • 比如缺了分号.
  • php -l 虽然我不知道咋用.
  • textmate和atom sublime应该都有对应的插件.

文件

  • 打开文件之前, 查看一下文件是否可以打开, 这个其实是判断一下open是否返回false.
  • php的io是阻塞的, 因此, 如果多个php任务, 最好使用多个不同的文件.

php源码研究

对于php学习有比较大的帮助:

  • [http://www.hoohack.me]
  • [http://www.php-internals.com/book/?p=chapt04/04-04-anonymous-function]

php大神的blog

http://www.laruence.com/2008/11/20/630.html