0

I found the following line of code in a PHP script and have never seen anything like it before:

$a = ($ba%10)

What does this do?

Lee Loftiss
  • 3,035
  • 7
  • 45
  • 73

3 Answers3

4

Its is PHP's Arithmetic Operators

enter image description here

The result of the modulus operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a. For example:

<?php

echo (5 % 3)."\n";           // prints 2
echo (5 % -3)."\n";          // prints 2
echo (-5 % 3)."\n";          // prints -2
echo (-5 % -3)."\n";         // prints -2

?>

Click PHP.NET for more information!

webGautam
  • 555
  • 1
  • 3
  • 14
3

It tells you the remainder of a division calculation. So 25%8 would be 1. If $ba = 101 then $ba%10 would equal 1.

dewd
  • 4,380
  • 3
  • 29
  • 43
  • Thanks Dewd. And thanks for not being like the losers who downvoted this question. – Lee Loftiss Jun 02 '13 at 09:57
  • @Lee A little passive aggressive, aren't we, dewd? – deceze Jun 02 '13 at 09:57
  • Sorry, I thought it was aggressive-aggressive. Well, it just sucks when people, who know way more about PHP than a lot of us, can't understand that sometimes the answers are not so clear. And instead, criticize you for not knowing. Downvoting is actually more passive aggressive since the cowards can just do it with no record of who they are or why they actually did it. – Lee Loftiss Jun 02 '13 at 10:03
  • 2
    There's no reason to downvote the question. – Glitch Desire Jun 02 '13 at 10:05
  • 1
    @Lightning Hover over the downvote arrow and read what it says. I think it applies. – deceze Jun 02 '13 at 10:06
  • Actually, it is incorrect. If you don't know that it is an operator, you don't know where to start. I actually did do several searches and spent at least 15 minutes trying to do searches that would return the results. I did notice just now, that 'what is % in php' does return a link to the link Deceze posted. However, the title does not make it clear that the answer is actually there. – Lee Loftiss Jun 02 '13 at 10:11
3

% is the modulus operator, it gives you the remainder of integer division.

e.g. 87 % 10 = 7

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39