PHP基础(三)===vs==

The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.

Examples:

1 === 1: true
1 == 1: true
1 === "1": false // 1 is an integer, "1" is a string
1 == "1": true // "1" gets casted to an integer, which is 1
"foo" === "foo": true // both operands are strings and have the same value

Warning: two instances of the same class do NOT match the === operator. Example:

$a = new stdClass();
$a->foo = "bar";
$b = clone $a;
var_dump($a === $b); // bool(false)

=== will only return true if both operands are the same type and the values are equal  ===必须值跟类型都一样才返回true

== 值相等的情况下就返回true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP的三大特性是封装、继承和多态。 1. 封装:将数据和方法封装在一个类中,对外部隐藏实现细节,只暴露必要的接口供外部使用。这样可以提高代码的可维护性和可重用性。 代码示例: ```php class Person { private $name; private $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function getName() { return $this->name; } public function getAge() { return $this->age; } } $p = new Person('Tom', 20); echo $p->getName(); // 输出 Tom echo $p->getAge(); // 输出 20 ``` 2. 继承:子类可以继承父类的属性和方法,并且可以在此基础上进行扩展和重写。这样可以减少代码的重复性,提高代码的复用性。 代码示例: ```php class Animal { protected $name; public function __construct($name) { $this->name = $name; } public function run() { echo $this->name . ' is running'; } } class Dog extends Animal { public function bark() { echo $this->name . ' is barking'; } } $d = new Dog('Tom'); $d->run(); // 输出 Tom is running $d->bark(); // 输出 Tom is barking ``` 3. 多态:同一种行为在不同的对象中具有不同的表现形式。不同的对象可以对同一种方法进行不同的实现,从而实现不同的行为。 代码示例: ```php class Animal { protected $name; public function __construct($name) { $this->name = $name; } public function run() { echo $this->name . ' is running'; } } class Cat extends Animal { public function run() { echo $this->name . ' is running quietly'; } } class Dog extends Animal { public function run() { echo $this->name . ' is running loudly'; } } $a = new Animal('Tom'); $c = new Cat('Jerry'); $d = new Dog('Spike'); $a->run(); // 输出 Tom is running $c->run(); // 输出 Jerry is running quietly $d->run(); // 输出 Spike is running loudly ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值