php -i查看 php.ini 配置文件
不输出错误原因:可能是 php.ini 导致的 (log 的配置问题, grep 配置排查下)
类型
- string
- int
- float
- bool
类型断言
is_string($x)is_bool($x)
类型转换
$str = "123";$int = 2020;$float = 99.0;$bool = false;var_dump((int) $str);var_dump((bool) $str);var_dump((string) $str);var_dump((bool) $str);var_dump((int) $float);var_dump((string) $float);var_dump((string) $bool);var_dump((int) $bool);
数组
var_dump($arr)查看详细的数据,包括类型print_r($arr)查看数据,隐去类型
追加数据:
$arr = [];$arr[] = "Java";$arr[] = "Python";$arr[] = "Golang";print_r($arr);// 遍历数据foreach ($arr as $item){printf("%s\n", $item);}
Map
$strangeMap = ["dog" => "狗","cat" => "猫",];$strangeMap["fish"] = "鱼";print_r($strangeMap);// 遍历foreach ($strangeMap as $k => $v){printf("%s => %s\n", $k, $v);}
运算符
php 是弱类型 ===``!==|| &&
函数
// 申明变量与返回值的类型$add = function (int $x, int $y): int{return $x + $y;};printf("=> %d\n", $add(2, 3));// 引用传递function add(int &$a, int $b) {$a += $b;}
异常处理
try {printf("exception demo!!!");} catch (Throwable $e) {Logger::info("Exception: %s\n", $e->getMessage());}
自定义异常:
面向对象
- 对于通过 public 声明的属性和方法,在类以外和继承类中均可见;
- 对于通过 protected 声明的属性和方法,仅在继承类(支持多个层级)中可见,在类以外不可见;
对于通过 private 声明的属性和方法,仅在当前类内部可见,在继承类和类之外不可 ```php abstract class Car { var $brand; var $wheelNum; var $maxSpeed;
/**
- Car constructor.
- @param $brand
- @param $wheelNum
@param $maxSpeed */ public function __construct(string $brand, int $wheelNum, int $maxSpeed) { $this->brand = $brand; $this->wheelNum = $wheelNum; $this->maxSpeed = $maxSpeed; }
public function run(): void { printf(“%s is running\n”, $this->brand); } }
class Tesla extends Car {
/*** Tesla constructor.* @param $brand*/public function __construct(string $brand){parent::__construct($brand, 4, 120);}
}
不要使用继承,使用面向接口编程```phpinterface Car{public function run(): void;}class CarFramework{var $wheelNum;var $maxSpeed;/*** CarFramework constructor.* @param $wheelNum* @param $maxSpeed*/public function __construct($wheelNum, $maxSpeed){$this->wheelNum = $wheelNum;$this->maxSpeed = $maxSpeed;}}class Tesla implements Car{var $carFramework;var $brand;/*** Tesla constructor.* @param $carFramework* @param $brand*/public function __construct($carFramework, $brand){$this->carFramework = $carFramework;$this->brand = $brand;}public function run(): void{printf("%s is running.", $this->brand);printf("Info => wheel: %d, max speed: %d\n",$this->carFramework->wheelNum,$this->carFramework->maxSpeed);}}// using$tesla = new Tesla(new CarFramework(4, 120), "Tesla");$tesla->run();
