How to use type operators in php operators

醉折花枝作酒筹
Release: 2023-03-11 22:46:01
Original
1672 people have browsed it

In the previous article, we learned about the decrement operator. If you need it, please read "Do you know this operator? 》. This time we introduce to you another operator, the type operator. You can refer to it if you need it.

When we read this article, will we be surprised, are there type operators in PHP? What it is? What can it be used for? Today we will take a look at what this operator can do.

The type operator refers to instanceof. The instanceof operator was introduced in php5. Before this, is_a() was used, but is_a() is obsolete. It is better to use instanceof.

Let’s take a look at a small example to learn this operator.

<?php
class other
{
}
class another
{
}
$a = new other;
var_dump($a instanceof other);
var_dump($a instanceof another);
?>
Copy after login

The result of this example is

How to use type operators in php operators

This result is quite interesting. There are only two possibilities: true and false. Let’s look at the code again. It First, two classes are defined, then $a is other, and then it is asked whether $a belongs to this class. I feel that this code means this.

Let’s see if this is what it means and introduce this operator in detail.

instanceof is used to determine whether a PHP variable belongs to an instance of a certain class. Returns true if it belongs, false if it does not.

Now that we have seen this application, let’s look at another use of this operator.

Let’s look at the example first.

<?php
interface other
{
   public function cmcc();
 }
 class ExampleClass implements other
{
   public function cmcc()
   {
     return &#39;Hello World!&#39;;
   }
 }
$exampleInstance = new ExampleClass();
 if($exampleInstance instanceof other){
   echo &#39;是的,它实现了!&#39;;
 }else{
   echo &#39;不,它没有实现.&#39;;
} 
?>
Copy after login

The result of this example is

How to use type operators in php operators

There are only two results of this example, one is "OK" and the other is "negative". Nothing more than these two results. Let's look at another use of this operator.

This operator can be used to determine whether an object implements an interface. Returns true if it belongs, false if it does not. Obviously this example shows that this object implements a certain interface.

That’s all. If you want to know anything else, you can click here. → →php video tutorial

The above is the detailed content of How to use type operators in php operators. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template