Php/keyword as
来自菜鸟教程
PHP作为关键字
例
运用
as
in a
foreach
环:
<?php $list = [1, 2, 3, 4]; foreach($list as $item) { echo $item; echo "<br>"; } ?>
定义和用法
The
as
关键字由
foreach
循环以确定哪些变量包含元素的键和值。
The
as
关键字也可以由名称空间和特征使用,以为其赋予别名。
相关页面
The
foreach
关键词。
The
trait
关键词。
The
use
关键词。
了解更多关于循环的信息 PHP循环教程
.
在我们的文章中了解更多关于特质的信息 PHP OOP-特性教程
.
更多例子
例
运用
as
in a
foreach
循环遍历关联数组:
<?php $people = [ "Peter" => "35", "Ben" => "37", "Joe" => "43" ]; foreach($people as $person => $age) { echo "$person is $age years old"; echo "<br>"; } ?>
例
运用
as
给a的方法起别名
trait
<?php trait message1 { public function msg1() { echo "OOP is fun! "; } } class Welcome { use message1 { message1::msg1 as msg; } } $obj = new Welcome(); $obj->msg(); ?>
例
运用
as
给一个别名
namespace
<?php namespace Html; class Table { public $title = ""; public $numRows = 0; public function message() { echo "<p>Table '{$this->title}' has {$this->numRows} rows.</p>"; } } use \Html as H; $table = new H\Table(); $table->title = "My table"; $table->numRows = 5; $table->message(); ?>