When formatting a number in php, pay attention to the number range.

Source: Internet
Author: User

Compare to construct SQL statements
Copy codeThe Code is as follows:
$ SQL = 'select *
FROM sdb_comments
WHERE goods_id = '. intval ($ goods_id ).'
AND for_comment_id IS NULL
AND object_type = ". $ item ."
AND disabled = "false"
AND display = "true "';

I prefer to do this:
Copy codeThe Code is as follows:
$ SQL = sprintf ('select *
FROM sdb_comments
WHERE goods_id = %. 0f
AND for_comment_id IS NULL
AND object_type = "% s"
AND disabled = "false"
AND display = "true" ', (float) $ goods_id, $ item );

This statement is still simple. If it is more complex, concatenating strings is a nightmare.

The second method is more convenient, but a small problem: when formatting a number, you need to pay attention to its value range. Numeric operation. The SQL statement returned at the end is not what we need.

I made a summary today:

% D: 2 ^ 31 ~ 2 ^ 31-1 (-2147483648 ~ 2147483647) (convert int to signed decimal)

% B: binary (convert int type to binary)

% C: character (convert int type to character)

% U: 2 ^ 32-1 (0 ~ 4294967295) (convert int to signed decimal)

% F:-2 ^ 128-(-3.4E38 ~ + 3.4E38) (convert float to float) Localization

% F:-2 ^ 128-(-3.4E38 ~ + 3.4E38) (convert float to float) Non-localization

% O (convert int to octal)

% S: String

% X: converts an int into a hexadecimal string of lowercase letters.

% X: hexadecimal format for converting an int to an uppercase letter

Because the id in the database may be large. If % d is used, the correct result may not be returned because it is out of the range. Therefore, it is recommended that %. 0f be better than % d When formatting the id.

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.