What Does the Percent Sign Mean in PHP

What does the percent sign mean in PHP?

It's the modulus operator, as mentioned, which returns the remainder of a division operation.

Examples: 3%5 returns 3, as 3 divided by 5 is 0 with a remainder of 3.

5 % 10 returns 5, for the same reason, 10 goes into 5 zero times with a remainder of 5.

10 % 5 returns 0, as 10 divided by 5 goes exactly 2 times with no remainder.

In the example you posted, (3 - 2 + 7) works out to 8, giving you 8 % 7, so $number will be 1, which is the remainder of 8/7.

what does the percent sign do in PHP?

PHP doesn't intrinsically treat it specially within strings. It must be the application that's parsing the strings and deciding that % indicates some kind of variable value.

In fact, it looks like you're looking at the source code of a project called osTicket. In its installer, I found this line which corresponds to your given define:

$configfile= str_replace('%CONFIG-PREFIX',$_POST['prefix'],$configfile);

Percent Sign Inside PHP Variable

You're using incorrect quoting

$Where = "WHERE User.FirstName LIKE '%$Search[0]%' OR User.LastName LIKE '%$Search[0]%'";
^----note the difference.

% is the modulo operator in PHP. Since you're using single quotes in your version, you're actually trying to take the modulo of some SQL text and a variable.

Reference — What does this symbol mean in PHP?

Incrementing / Decrementing Operators

++ increment operator

-- decrement operator

Example    Name              Effect
---------------------------------------------------------------------
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.

These can go before or after the variable.

If put before the variable, the increment/decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment/decrement operation is done.

For example:

$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}

Live example

In the case above ++$i is used, since it is faster. $i++ would have the same results.

Pre-increment is a little bit faster because it really increments the variable and after that 'returns' the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second's.

However, you must use $apples--, since first, you want to display the current number of apples, and then you want to subtract one from it.

You can also increment letters in PHP:

$i = "a";
while ($i < "c") {
echo $i++;
}

Once z is reached aa is next, and so on.

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.


Stack Overflow Posts:

  • Understanding Incrementing

Why does PHP print a percent sign when my script is done?

This is from zsh.

Your output doesn't end with a line break. Bash would start the PS1 right after your output, zsh prints a (colored) % and insert a line break itself. You can prevent it by adding a line break yourself

php -r 'echo "Hello World\n";'

Note: I switched " and ', in php '\n' would print it as is but "\n" means line break.

percent sign in PHP string

Thanks for your response.
My problem was, $_REQUEST['var1'] was a number.
%number is parsed under HTML.
For error 500, It was mistake in my code.

What does the percentage sign mean in Python

Modulus operator; gives the remainder of the left value divided by the right value. Like:

3 % 1 would equal zero (since 3 divides evenly by 1)

3 % 2 would equal 1 (since dividing 3 by 2 results in a remainder of 1).

Preg_match and percent sign

this preg_match actually let strings with "%" (percent signs) through and it shouldn't. Why?

That is due to unescaped hyphen in the middle of your regex:

'#[^0-9 -&()+@._A-Za-z]#'
--------^

- is acting as range from space (32) to & (38) thus matching anything in between including % ( 37).

It should be used as:

'#[^-0-9 &()+@._A-Za-z]#'

Or

'#[^-\w &()+@.]#'

However without anchors this character class will match only one character. You should use:

'#^[^-\w &()+@.]+$#'


Related Topics



Leave a reply



Submit