In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "PHP comparison operation and logic operation example explanation". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. The following values are judged as true with empty ():
Values returned by unassigned variables, undeclared variables, 0, "0", "false, null, empty array array (), object's magic method _ _ get ()
In versions lower than PHP5.0, objects without any attributes are also judged as true by empty.
Note: empty () only accepts the index or attribute values of variables or variables, and cannot pass in constants or operation expressions directly. Expressions are supported after PHP 5.5.
2. The value judged as false by isset (): the value returned by unassigned variable, undeclared variable, null, _ get (), just like empty (), cannot be constant and expression.
3. Comparison of different types of data
If one is boolean or null, convert it to boolean comparison
Otherwise, if one is number, convert it to number comparison
Otherwise, if one is string, convert it to string comparison
Object types are always greater than array types and scalar types, and array types are always larger than scalar types.
Note these comparison results:
Numeric strings starting with / / 0 are not converted to octal, but are simply discarded. Compare'0' discarded by digits: 123 'discarded 0123' / / true "123" true; / / false 2 numbers true; / / true null==0; / / true-1k); / / falseecho json_encode ((bool) ($obj- > k)); / / true
Several scenarios in which string is converted to number:
Echo 'abc'*1; / / 0echo' 012 numbers 1; / / 12 multiplication: hexadecimal numbers can be converted, 0echo '0x12.123 numbers are returned if not the beginning of the number; / / 18echo (float)' 0x12'; / / 0echo (int) '0x12'; / / 0 cannot handle hexadecimal numbers echo (float) '12abcnumbers; / / 12 intercept the left string echo (float)' abc' / / 0 is not a number to return 0is_numeric ('0x123'); / / true can recognize the hexadecimal number is_numeric (' 0x123.123'); / / false recognizes that the target is the entire string rather than intercepting the previous part
When string converts to number, the numeric string on the left is intercepted for conversion, and if not, 0. 0 is returned.
Other data is converted to string:
/ / the value of several transferred strings (string) 0; / / "0" (string) true; / / "1" (string) false; / / "" (string) null; / / "(string) array (); / /" Array "
Arrays can perform string concatenation operations directly but cannot perform mathematical operations.
Conversion of object types to boolean is always true, while object types cannot be converted to number and string, so string concatenation and mathematical operations cannot be performed.
The way to convert scalars to array is to set the first element of the array to a scalar and return the array.
The scalar is converted to object to get an instance of the stdClass class, and the value of the scalar is assigned to the property named scalar: Object ([scalar] = > 234)
Array turns object to get an instance of the stdClass class, and the key of the array is the attribute name of the strength.
The transition from object to array is a bit complicated:
Methods, static properties, class constants are discarded
The protection attribute name is preceded by a "*"
The private attribute is prefixed with the class name (the case is exactly the same as the class name)
These prefixes are preceded by the sky character\ 0
For example, an array converted from object is:
Array ([* v] = > 444 [bf] = > 333 [bk] = > 99977 [Ak] = > 999 [* p] = > 888 [a2] = > 22)
Among the original objects are:
Public attributes a2, protected attributes v, p, which class these attributes come from cannot be identified (if overridden, take the properties of the subclass)
Private attributes f, k from class b (from the array key, taking bf as an example, it is impossible to tell whether the attribute name is bf or from the private attribute f of class b)
Private attribute k from class A
It is impossible to distinguish between b and A which is the subclass and which is the parent class (only from the key of array, it is impossible to infer which class the original object is constructed from)
Example:
Class A {private $A = 'private property, $An of class A'; / / This will become'\ 0A\ 0A 'protected $C =' protected property, $C of class A';} class B extends A {private $A = 'private property, $An of class B'; / / This will become'\ 0B\ 0A 'public $AA =' public property, $AA of class B'; / / This will become 'AA' protected $B =' protected property, $B of class B';} $arr = (array) new B (); foreach ($arr as $key = > $value) {echo'' Echo $key.', length: '.strlen ($key). Value:'. $value;}
Output result:
BA,length: 4 value: private property, $An of class BAA,length: 2 value: public property, $AA of class Bamboo B value length: 4 value: protected property, $B of class BAA,length: 4 value: private property, $An of class Achievement C Journal length: 4 value: protected property, $C of class A
5. Logic operations always return true or false (those who have written too much javascript should note). The priority of logic operators from high to low is & &, |, and, or. The short-circuit effect of logic operators can be used in statements, but remember that they will not return a value that is not a boolean type as in javascript. Be careful when using them in expressions.
/ / 1$ b | | $a = 200 * * echo $a; / / 200 |
6. The comparison of switch is not "=" but "=" (in javascript, it is "= =")
7. In php4, the comparison between object is the same as that of array. In php5, the "= =" between object types is more than the previous mention of true is that they belong to instances of the same class (of course, attributes are compared, which is similar to scalar "=" comparison), and "=" between object is more true than the premise that they are the same object.
In PHP4, an object that does not include any member variables is judged as true by empty ()
String offset offset character empty () decision: take the corresponding offset characters to judge, before PHP5.4, use the index to take characters from the string will be rounded first, so the left strings that do not contain numbers are converted into 0PHP5.4, the non-shaping format of the string index is no longer rounded, so it is judged as true, similarly, isset () is determined as false. Such as:
$str = 'ab0d';empty ($str [0]); / / falseempty ($str [0.5]); / / the false index is rounded down to 0empty ($str ["0.5"]); / / the false index is rounded down to 0meme PHP 5.4 without forensics, which is determined to be true empty ($str [2]); / / true, the character obtained is "0" empty ($str ["3"]). / / false, the character obtained is "d" empty ($str [4]); / / true, the index is out of range, notice warning, but empty () ignores the warning empty ($str ['a']); / / false, the left side does not contain a numeric string index PHP5.4 is processed as $str [0], after PHP5.4, it is directly for the decision true
Whether it is "not equal" or "=", do not use "transitivity" in PHP cross-type data comparisons:
$a = $b; / / true
$b = $c; / / true
Does not mean that $a = $c is true
Comparison method of array
/ / an array is a function standard_array_compare ($op1, $op2) {if (count ($op1) compared in this way using the standard comparison operator).
< count($op2)) { return -1; // $op1 < $op2 } elseif (count($op1) >Count ($op2) {return 1; / / $op1 > $op2} foreach ($op1 as $key = > $val) {if (! array_key_exists ($key, $op2)) {return null; / / uncomparable} elseif ($val)
< $op2[$key]) { return -1; } elseif ($val >$op2 [$key]) {return 1;}} return 0; / / $op1 = = $op2}
8. Ternary operators: unlike most other programming languages, PHP's ternary operators are left-bound!
$arg = 'titled; $vehicle = (($arg = =' B')? 'bus': ($arg = =' A')? 'airplane': ($arg = ='T')? 'train': ($arg = =' C')? 'car': ($arg = =' H')? 'horse':' feet'); echo $vehicle; / / horse
The ternary operation expression is divided into
($arg = ='B')? 'bus': ($arg = =' A')? 'airplane': ($arg = ='T')? 'train': ($arg = =' C')? 'car': ($arg = =' H')? This is the end of the introduction of 'horse':' feet'; an example of PHP's comparative operation and logical operation. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.