PHPクイックエントリー-基本構文およびオブジェクト向け

29493 ワード

sublimeの構成
{
    "cmd": ["php", "$file"],
    "file_regex": "php$",
    "selector": "source.php"
}

コードインスタンス
  
echo "hello world"

#     
//     
/*
        
*/
?>

基礎文法
 

//       :                          
// PHP           ($)
//     
$name = "Tom";
echo $name;

//         
$detail = "
this is first line this is second line
"
; echo $detail; // $a = 20; function global_func(){ // print($a); // Undefined variable global $a; // print($a); // 20 $GLOBALS['a'] += 30; // echo "
"
; } global_func(); print($a); // 50 echo "
"
; // function static_func(){ static $a = 1; echo "a="; echo $a; echo "
"
; $a += 1; } static_func(); // a=1 static_func(); // a=2 static_func(); // a=3 // define("MAX", 20); // print MAX; // 20 print "
"
; print(constant("MAX")); // 20 print "
"
; // PHP == ===( ) <> != !==( ) ?>
// if (1 < 2){ print("1<2"); } else{ print("1>=2"); } print "
"
; // 1<2 // // 4 :for、while、do...while、foreach, 3 C for($i=1; $i<=3; $i++){ print($i); print "
"
; } // 1 2 3 $i = 1; while ($i<=3) { print $i; print " "; $i++; } print "
"
; // 1 2 3 do{ print $i; print " "; $i--; } while($i>0); print "
"
; // 4 3 2 1 // , $list = array(1, 2, 3, 4, 5); foreach ($list as $value) { print "value = $value "; } print "
"
; // value = 1 value = 2 value = 3 value = 4 value = 5 // 3 // $numbers = array(1, 2, 3, 4, 5); foreach ($numbers as $value) { print("$value "); } print "
"
; // 1 2 3 4 5 // $numbers[0] = "one"; $numbers[1] = "two"; $numbers[2] = "three"; foreach ($numbers as $value) { print("$value "); } print "
"
; // one two three 4 5 // $names[0] = 1; $names[2] = 2; foreach ($names as $value) { print("$value "); } print "
"
; // 1 2 // $length = count($numbers); for($i=0; $i<$length; $i++){ print($numbers[$i]); print(" "); } print "
"
; // one two three 4 5 // PHP 5.5.36, python $people = ["Tom"=>"20", "Jack"=>"30", "Jimi"=>"40"]; // print($people["Tom"]); print(" "); print($people["Jack"]); print(" "); print($people["Jimi"]); print("
"
); // 20 30 40 // $people["Tom"] = 50; $people["Jack"] = 60; $people["Jimi"] = 70; // foreach ($people as $key => $value) { print("$key=>$value
"
); } print("
"
); // Tom=>50 // Jack=>60 // Jimi=>70 // $cars = array( "first" => [ "name" => "car1", "color" => "white" ], "second" => [ "name" => "car2", "color" => "black" ]); // print($cars["first"]["name"]); # car1 print("
"
); // PHP /* sort() - rsort() - asort() - , ksort() - , arsort() - , krsort() - , */ // // // // (.): $a = "hello"; $b = "world"; print($a . " " . $b); // hello world print("
"
); // $c = " "; print(strlen($a)); // 5 print(strlen($c)); // 6 // print(strpos($a, "ll")); // 2 print(strpos($a, "xx")); // FALSE print("
"
); // // $a , &$b , $c function func_args($a, &$b, $c=2){ $a += 1; $b += 1; $c += 1; print("a=$a b=$b c=$c
"
); // a=2 b=2 c=3 return $a; // } $args_a = 1; $args_b = 1; $ret = func_args($args_a, $args_b); print("ret=$ret
"
); // ret=2 print("args_a=$args_a args_b=$args_b
"
); // args_a=1 args_b=2 ?>

オブジェクト向け
 

//    PHP      

/*
    
public(  ):                。
protected(   ):                        。
private(  ):                   。
*/
//           ,   ,    ,      , var   ,      
// Final    
//        final,          。
//           final,      。

class Father{
    public $name = "Tom";
    protected $age = 40;
    private $address = "  ";

    function print_info(){
        print("public name: " . $this->name . "
"
); print("protected age: " . $this->age . "
"
); print("private address: " . $this->address . "
"
); } final function work(){ print(" , ,
"
); } } // class Child extends Father{ protected $age = 20; // protected function print_info(){ print("public name: " . $this->name . "
"
); print("protected age: " . $this->age . "
"
); // print("private address: " . $this->address . "
"); // Undefined property
} // function work(){ // print("
");
// } // Cannot override final method } $father = new Father(); $father->print_info(); /* public name: Tom protected age: 40 private address: */ $child = new Child(); $child->print_info(); /* public name: Tom protected age: 20 */ // class People{ function __construct($name){ $this->name = $name; print(" " . $this->name); } function __destruct(){ print(" "); } } class Human extends People{ function __construct($name){ parent::__construct($name); // print(" " . $this->name); } } $people = new People(" "); print("
"
); /* */ $human = new Human(" "); print("
"
); // // // , // , // interface ISleep{ public function sleep($time); } interface IEat{ public function eat(); } // class Dog implements ISleep, IEat{ public function sleep($time){ print("sleep... time: $time
"
); } public function eat(){ print("eating ...
"
); } } $dog = new Dog(); $dog->sleep(5); $dog->eat(); /* sleep... time: 5 eating ... */ // // , , 。 // 。 // , abstract class AbsBase{ // abstract public function sleep(); // public function eat(){ print("eat...
"
); } } class Cat extends AbsBase{ public function sleep(){ print("cat sleep...
"
); } } class Pig extends AbsBase{ public function sleep(){ print("pig sleep...
"
); } } // $abs = new AbsBase(); // Cannot instantiate abstract class $cat = new Cat(); $cat->eat(); // eat... $cat->sleep(); // cat sleep... $pig = new Pig(); $pig->eat(); // eat... $pig->sleep(); // pig sleep... // static // class Foo{ public static $name = "static name"; public function get_name(){ print(self::$name); // print("
"
); } } print(Foo::$name); // static name // $foo = new Foo(); $foo->get_name(); // static name ?>

参考PHPクイックエントリー