データの型を確認
var_dump($msg);
定数
var_dump(__LiNE__); var_dump(__FILE__); var_dump(__DIR__);
文字列の連結
$s = "hello " . "world";
真偽値
$x = 5; if ($x){ echo "great"; }
三項演算子
$max = ($a > $b) ? $a : $b;
配列
$sales = array( "yama" => 200, "sato" => 400, "ito" => 600, ); var_dump($sales["sato"]);
コロン構文
<ul> <?php foreach ($colors as $value): ?> <li><?php echo "$value"; ?></li> <?php endforeach; ?> </ul>
クラス(オーバライドの禁止はメソッド名の前にfinalを付けます)
class User{ public $name; public function __construct($name){ $this->name = $name; } public function sayHi(){ echo "hi, i am $this->name!"; } } $tom = new User("Tom"); $bob = new User("Bob"); echo $tom->name; $bob->sayHi();
抽象クラス
abstract class BaseUser{ public $name; abstract public function sayHi(); } class User extends BaseUser{ public function sayHi(){ echo "hello from user"; } }
インターフェイス
interface sayHi { public function sayHi(); } interface sayHello { public function sayHello(); } class User Implements sayHi, sayHello { public function sayHi{ echo "hi"; } public function sayHello{ echo "hello"; } }