DeTechn Blog

PHP基本语法学习

<?php

echo 'hello<br>';
//phpinfo();
echo date('Y-m-d');


$func = true;
var_dump($func);

$int = 5;
echo 'hello' . gettype($func)&nbsp;.&nbsp;&#39;&lt;br&gt;&#39;&nbsp;.&nbsp;$int;

if (is_array($int)) {
    echo 'is array';
} else {
    echo 'is not array';
}

$i = 1.111112;
$ii = 3.14;
echo $i&nbsp;.&nbsp;&#39;,&#39;&nbsp;.&nbsp;gettype($i);

echo '<br>
break usage
    what do you like?
';

echo '<br> \print&#39;danyinh&#39;<br>';

echo "abc$i${ii}i<br>";

$str = <<<EOF
    hello eof
are you ok?$i<br>'dkdk'
EOF;

echo "strange string output $str <br>";

//serialize($str);

$arr = ['a' => 'b', 'c', 'b', 'c' => 'd', 'c'];
echo $arr['a'] . '<br>';
var_dump($arr);
echo $arr[0] . '<br>';

class foo {
    function printLog() {
        echo "log print <br>";
    }
}

$f = new foo;
$f->printLog();


$obj = (object)'hello sang';
var_dump($obj);
$var_arr&nbsp;=&nbsp;get_object_vars($obj);
foreach ($var_arr&nbsp;as&nbsp;$k => $v) {
    echo "$k:$v<br>";
}
echo '$obj&nbsp;class&nbsp;is&nbsp;:&#39;&nbsp;.&nbsp;get_class($obj) . '<br>';

$obj_from_arr&nbsp;=&nbsp;(object)$arr;
var_dump($obj_from_arr);

//---- 传值&传引用 ------//

$var1 = 'abc';
$var2 = 12;

echo "var1:$var1&quot;&nbsp;.&nbsp;&quot;,var2:$var2<br>";
$var3&nbsp;=&nbsp;$var1;
echo "var3:$var3";
$var1 = 'ab';
echo ",by_value_var3:$var3";
$var3&nbsp;=&nbsp;&amp;$var1;
echo ",by_ref_var3:$var3";

//---------- 预定义变量 ------
var_dump($GLOBALS);

//---- 表单提交例子 ---------
var_dump($_POST);
?>

    <html>
    <body>
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
        <input type="text" name="uname"/>
        <input type="submit" name="submit" value="save"/>
    </form>

    </body>
    </html>

<?php

//---------- 常量 -----------

define("TEST_MAIN", 'hello');
echo TEST_MAIN;

echo 'current file path : ' . __FILE__;

//--------- 数组运算符------------
$arr1 = array('1' => 'abc', '2' => 'def');
$arr2 = array('1' => '123', '3' => '456');
var_dump($arr1&nbsp;+&nbsp;$arr2);

//-------- instanceof ---
class A {
}

class B {
}

$a = new A;
echo $a instanceof A;

//------- 反引号----------------------

$list = ls&nbsp;-alh&nbsp;./;
echo $list;

//--- foreach遍历
foreach ($arr1&nbsp;as&nbsp;$k => $v) {
    echo $k&nbsp;.&nbsp;&#39;:&#39;&nbsp;.&nbsp;$v . '<br>';
}

foreach ($arr1&nbsp;as&nbsp;$v) {
    echo $v;
}

foreach ($arr1&nbsp;as&nbsp;&amp;$v) {//加上&修改数组原值
    $v = '111';
}

var_dump($arr1);


//-------------- 范围解析符(静态调用) ---------------------

class Test {
    function printMsg($msg) {//默认是public的
        echo $msg;
    }
}

$msg = 'hello world';
Test::printMsg($msg);

// ---------- 静态属性 静态方法 -----------------

class TestA {
    static $name = 'abc';

    function showName() {
        echo self::$name; //方法内部调用
    }
}

class TestB {
    var $name = 'abc';//必须带上修饰符比如 var, public,private,protected

    function showName() {
        echo $this->name;
    }
}

class TestC {

    const MAXSIZE = 12;//类里定义常量
    static $mtd = 'ad';
    var $mtd2 = 'abc';

    static function showMtd() {
        echo 'static method:' + self::$mtd;
    }

    function showMtd2() {
        echo 'normal method:' + $this->mtd;
    }

    function getConst() {
        echo self::MAXSIZE;
    }
}

TestC::showMtd();
//TestC::showMtd2();//此时this会报错,无法找到.和java还是区别很大
$tc = new TestC();

echo TestC::MAXSIZE;//访问类中常量


// --------------   类的继承 -----------------------

class Pa {
    var $a = 'you ok?';

    function show() {
        echo 'from parent;';
    }
}

class Ch extends Pa {

    function show() {
        echo 'from child;';
        //也指明再执行父亲方法
        parent::show();
    }
}

$ch = new Ch;
echo $ch->a;

$p = new Pa;
$c = new Ch;
$p->show();
$c->show();

//------------  构造函数 --------------
class T1 {
    function __construct() {
        echo 'abcdddd;';
    }
}

class T2 extends T1 {
    function __construct() { //不带参数构造函数,基类自动执行
        echo 'ddddddd;';
    }
}

$t2 = new T2;


class T3 {
    var $a;

    function __construct($a) {
        $this-&gt;a&nbsp;=&nbsp;$a;
    }
}

class T4 extends T3 {
    function __construct($a) {
        parent::__construct($a);//父子类构造函数带参,则必须显式调用父类构造函数!
    }
}


//------------------- 析构函数 --------
class T5 {
    var $a;// php4中用var,php5用public,但兼容var,参考http://www.kuitao8.com/20150427/3717.shtml

    function __destruct() {
        echo ou'do destruct;';
    }
}

$t5 = new T5;
unset($t5);

// ------------------ 序列化&反序列化 ---------------------
class T6{
    public $a;
    public $b;

    function __sleep() {
        return(array('a'));//只序列化a
    }

    function __wakeup() {
        $this->b = uniqid();//初始化未序列化的b
    }
}

$t6 = new T6;
$t6->a = 'sang;';
$t6s&nbsp;=&nbsp;serialize($t6);
$t6us&nbsp;=&nbsp;unserialize($t6s);

var_dump($t6us);

// -------------------  字符串操作 --------------------
$str1 = 'abcdefgHIGK   ';
echo $str1{0};
echo strlen($str1);
echo strlen(trim($str1));
echo strtoupper($str1);
echo strcmp($str1,$str);
echo substr($str1,0,2);//ab
echo substr_replace($str1,'11',0,2);
echo substr_replace($str1,'11',-1,-2);

echo strpos($str1,'e');

$str2 = 'a|b|c';
$str_arr&nbsp;=&nbsp;explode(&#39;|&#39;,$str2);//分割
var_dump($str_arr);
$str3&nbsp;=&nbsp;implode(&#39;@&#39;,$str_arr);//合并
echo $str3;

$str4 = 'http://blog.csdn.net/spy19881201/article/details/25537225?a=1&;b=2';
var_dump(parse_url($str4));

// ----------- 正则表达式 --------------
echo preg_match('/^a?b$/','ab');
echo preg_match('/^a?b$/','aab');
echo preg_match('/^-{0,1}[0-9]{0,}.{0,1}[0-9]{0,}$/','-1.2');
echo preg_match('/^-?[0-9].?[0-9]$/','-1.2');

//--------------------- 数组 -------------------------
$arr1 = array('a','b');//索引数组
$arr2 = array('1'=>'a','2'=>'b');//关联数组
var_dump($arr1);
var_dump($arr2);

$arr3 = array('a','1'=>'b','0' =>'c');//注意,如果key为整形字符串,比如'0',会转成索引数组.数组后面的值覆盖前面的值
var_dump($arr3);
echo $arr3['0'];

$arr4['a'] = 'b';//arr4不存在,则自动创建一个!!!

foreach ($arr2&nbsp;as&nbsp;$item){
    echo $item;
}

foreach ($arr2&nbsp;as&nbsp;$k => $v){
    echo $k.&#39;:&#39;.$v;
}

phpinfo();

?>

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »