For 2023 (and 2024 ...) - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
Assignment, equality and identity in PHP
In PHP, you'll find that there's an = operator (that's one = sign), an == operator (that's two equals signs) and an === operator (triple equals). The single = sign is an assignment - it tells PHP to work out the expression to the right of the = sign and save it to the variable / location named on the left. Both == and === perform comparisons - so what's the difference?
== (double equals) is an equality test - it checks whether the values to the left and right of the == operator have the same value - for example, it could check if they're both the value 10. It will return a true value, though, if you compare the integer 10 to the floaring point number 10.0, or if you compare either of those to the string "10.00" ... they're all the value 10, after all!
=== (triple equals) is an identity operator which checks if the values to the left and the right of the === operator have the same value AND are of the same type, so it can only return a true value if you compare two integers or two floats or two strings ... it is bound to return a false value if you compare a float to an integer, even if they both contain the value 10.
Example:
<?php
$first = 10;
$second = 10.0;
$third = "10";
if ($first == 10) print "One";
if ($second == 10) print "Two";
if ($third == 10) print "Three";
if ($third === 10) print "Four";
if ($second === 10) print "Five";
if ($first === 10) print "Six";
?>
Will print out
OneTwoThreeSix
(written 2005-08-08, updated 2006-06-05)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles H104 - PHP - Control Statements [340] Code and code maintainance efficiency - (2005-06-08) [353] Wimbledon Neck - (2005-06-20) [421] Don't repeat code - use loops or functions - (2005-08-21) [657] The ternary operator in Python - (2006-03-25) [863] Double and Triple equals operator in PHP - (2006-09-12) [962] Breaking a loop - Ruby and other languages - (2006-12-03) [1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18) [1199] Testing for one of a list of values. - (2007-05-22) [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06) [1477] Decisions - small ones, or big ones? - (2007-12-18) [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04) [1825] Question Mark - Colon operator (Perl and PHP) - (2008-10-08) [2304] Extracting real data from an exported file in PHP or Perl - (2009-07-25) [2912] Predictions for the seagull population - (2010-08-09) [3200] How a for loop works Java, Perl and other languages - (2011-03-12) [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18) [3895] Flowchart to program - learning to program with Well House - (2012-10-14) [3914] While, for, foreach or something else to loop. - (2012-11-06) [4322] Learning to Program - the conditional statement (if) - (2014-11-21) [4323] Learning to program - Loop statements such as while - (2014-11-22)
Some other Articles
Reading a news or blog feed (RSS) in your PHP pageFunctions and commands with dangerous namesCan an older person learn a programming languageTheft of training materialAssignment, equality and identity in PHPHorse's Mouth is a year oldHow to check that a string contains a number in TclFull circle - made it back to an old hauntNetlessWhat is an SQL injection attack?
|
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|