Explanation of the strange phenomenon of PHP character variables

Source: Internet
Author: User
Tags ord

First of all, the magic of PHP is supported by character data types, some students wrote such a code:

for ($c = ' a '; $c <= ' z '; $c + +) {
Echo $c. ' ';
}

But it turns out it's not the A to Z he wants.
A b c d e F g h i j k l m n o p q R S t u v w x y z aa ab AC ad AE af ag ah ai
AJ Ak al am a AO ap aq ar as at au av aw ax ay az ba bb BC BD AM BF bg BH
BI BJ BK bl bm bn bo BP bq br bs BT BU BV bw BX by BZ CA CB CC CD CE CF CG
Ch CI cj CK cl cm CN CO CP CQ CR CS CT CU CV cw cx cy CZ da DB DC dd de DF
dg DH di dj DK dl DM DN Do DP dq Dr ds dt du dv DW dx dy DZ ea EB ec Ed Ee
EF eg eh ei ej ek el em en eo ep eq er es et eu ev ew ex ... On to YZ

There are more than n ways to achieve A to Z print, such as the following
for ($i = ' a '; $i!= ' AA '; $i + +)
Echo $i. ' ';


for ($i = Ord (' a '); $i <= ord (' z '); $i + +)
echo chr ($i). ' ';


echo implode (', Range (' A ', ' Z '));

But we're going to have to explain what kind of ghosts are on the print list.
This is because in the arithmetic operation of a single character variable, PHP follows the usage of Perl rather than c.
In Perl
$a = ' Z ';
$a + +; ' AA '

In C
A = ' Z ';
a++; ' [' because the ASCII value of ' Z ' is 90, ' [' The ASCII value is 91

Note that character variables can only be incremented, cannot be decremented, and only plain letters (A-Z and A-Z) are supported and are visible as follows:
$a = "9F9";
$b = "9F9";
Var_dump (+ + $a); "9g0"
Var_dump (--$b); "9F9" does not change

And then there's this kind of classmate crying.
$a = "9E0";
Var_dump (+ + $a); Float (10)

#这TM又变成科学计数法了啊 ...

PHP never sets the cards by the routine!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.