Results 1 to 8 of 8

Thread: The Myriad Uses of the "%" sign

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default The Myriad Uses of the "%" sign

    I have seen the % sign used in a lot of different contexts. John Scheuer explained one of them as follows:

    There would almost never be a %3 in a query, perhaps a %3e, or %3E, or %20 - that one's really common, it's a space.

    The % sign just means that the next two characters make up a hex code that corresponds to the character being escaped (certain characters don't do well in URLs). If you had a chart of characters with their hex codes, that would tell you. Generally you don't need to know, they just represent characters that are in the link. You can always decipher any given one by making up a little script, ex:

    Code:
    alert(unescape('%3e')); Here's a fairly good table of hex (and other, use the hex column) codes for common characters:

    http://en.wikipedia.org/wiki/ASCII#A...ble_characters
    But now I have found another example that doesn't seem to fit under the explanation above. What does % mean used in this context?... thanks.

    Code:
    $i = $i % 7;

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    John was talking about URL escaping. You are talking about PHP. These are two completely different systems.

    In PHP, % is the modulo operator. $x % $y returns the remainder when $x is divided by $y.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. The Following User Says Thank You to Twey For This Useful Post:

    kuau (08-08-2008)

  5. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Yes... Divides the expression evenly, and returns the remainder.
    - Mike

  6. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    So $i = $i % 7; means $i = the remainder when $i is divided by 7?

    PS. To someone who is new to all this, can someone please explain how the edges of things are determined? Where does php end and url escaping start? Isn't url escaping part of php? How does one tell when an html phrase suddenly turns into javascript? or is that php? or sql? or maybe css? or xml? To the uninitiated it is all one big jumbled melee and I am never quite sure where to post my questions.

  7. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Um no? There $i doesn't hold a value.

    However:
    Code:
    $i = 5;
    $i = $i % 2
    Should equal 1. (5 /2, remainder 1)
    - Mike

  8. The Following User Says Thank You to mburt For This Useful Post:

    kuau (08-08-2008)

  9. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    I get it! Now I understand what the code in question is doing. Thanks very much.

  10. #8
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •