View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In this article, we will discuss the differences between ‘==’ and ‘===’ operators in PHP. Both are comparison operators used to compare two or more values.
== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false.
Syntax:
operand1 == operand2
=== Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.
Syntax:
operand1 === operand2
Note: === operator will return false when data types of operand are different.
Example 1: The following code demonstrates == operator with same and different data type operands.
PHP
Output:
Equal Equal
Example 2: The following code demonstrates the === operator.
PHP
Output:
Equal not Equal
Difference between == and === operators:
== | === |
It is equal to operator. | It is an identical operator. |
It is used to check the equality of two operands. | It is used to check the equality of both operands and their data type. |
Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. [Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.]
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
If you know, could you maybe give me some tags where I can use them in ?
3 Answers
Matthew Bilz January 27, 2016 6:30pm
As far as basic PHP is concerned, when you're echoing out a string enclosed in " ", you can use the variable inside the string without having to concatenate. For example:
$message = "Hello $first_name! Nice to meet you!";
With single quotes, you would write that code as:
$message = "Hello " . $first_name . "! Nice to meet you!";
Also, just make sure that any string you're echoing out that uses quotation marks is using the opposite version of the enclosing marks you started off with. For instance:
$message = "Click here to view cat videos.";
If you use double quotes surrounding the index.php above, it would end the first string and cause error.
I hope these couple of examples help!
Hi Beau, I just posted this same answer on a similar question, but it applies here too:
Reading the PHP manual can seem a bit cryptic if you are first learning the language, but it does have great explanations for the different types of syntax that can be used for specifying strings. There are actually 4 ways to specify strings [as of PHP 5.3.0]:
- single-quoted
- double-quoted
- heredoc syntax
- nowdoc syntax
There are pros and cons to using each. Check it out at the PHP manual: //php.net/manual/en/language.types.string.php#language.types.string.syntax.single They give descriptions and examples of use for each of them.
kajal pareek July 29, 2016 4:09pm