PDA

View Full Version : The Myriad Uses of the "%" sign



kuau
08-08-2008, 07:41 PM
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.


$i = $i % 7;

mburt
08-08-2008, 07:44 PM
Modulus operator:
http://www.w3schools.com/js/js_operators.asp

Twey
08-08-2008, 07:50 PM
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.

mburt
08-08-2008, 07:52 PM
Yes... Divides the expression evenly, and returns the remainder.

kuau
08-08-2008, 08:03 PM
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.

mburt
08-08-2008, 08:14 PM
Um no? There $i doesn't hold a value.

However:

$i = 5;
$i = $i % 2
Should equal 1. (5 /2, remainder 1)

kuau
08-08-2008, 09:23 PM
I get it! Now I understand what the code in question is doing. Thanks very much. :)

Jesdisciple
08-09-2008, 08:58 PM
kuau, see http://www.dynamicdrive.com/forums/showthread.php?p=156290.