PHP » PYTHON |
login |
register |
about
|
PYTHON Comparison Operators
is this article helpful?
|
Python replacement for PHP's Comparison Operators
[
edit
| history
]
a == b #Equal a is b #Identical: "a" and "b" are the same object a != b #Not equal a <> b #Not equal; this form is removed in Python 3.x a is not b #Not identical a < b #Less than a > b #Greater than a <= b #Less than or equal to a >= b #Greater than or equal to Ternary operator: action = 'default' if 'action' in _POST else _POST['action'] # Python 2.5+ Comparison OperatorsComparison operators, as their name implies, allow you to compare two values. You may also be interested in viewing the type comparison tables, as they show examples of various type related comparisons.
If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers. These rules also apply to the switch statement.
<?php
For various types, comparison is done according to the following table (in order).
Example #1 Transcription of standard array comparison
<?php
See also strcasecmp(), strcmp(), Array operators, and the manual section on Types. Ternary OperatorAnother conditional operator is the "?:" (or ternary) operator. Example #2 Assigning a default value
<?phpThe expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE. Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.
|
more
Recently updated
more
Most requested
|