Asked  7 Months ago    Answers:  5   Viewed   47 times

What exactly does this mean?

$number = ( 3 - 2 + 7 ) % 7;

 Answers

56

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.

Wednesday, March 31, 2021
 
rasmusx
answered 7 Months ago
44
foreach ($alphabet as $i => $letter) {
    echo (($i % 2) == 1 ? '!' : '') . $letter;
}

$i is the position of the letter in the array; since array indexes start from 0, even letters will have odd indexes.

$i % 2 is 0 when $i is even, 1 when it's odd. This is grade school arithmetic, which should be a prerequisite for a programming career.

Saturday, May 29, 2021
 
mattltm
answered 5 Months ago
95

It's a reference to the current object, it's most commonly used in object oriented code.

  • Reference: http://www.php.net/manual/en/language.oop5.basic.php
  • Primer: http://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.html

Example:

<?php
class Person {
    public $name;

    function __construct( $name ) {
        $this->name = $name;
    }
};

$jack = new Person('Jack');
echo $jack->name;

This stores the 'Jack' string as a property of the object created.

Tuesday, June 1, 2021
 
apokryfos
answered 5 Months ago
41

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).

Monday, June 7, 2021
 
mikelovelyuk
answered 5 Months ago
51

== means equality, so the conditional reads as:

If pre-incremented $x equals 10, echo $x

Single = is assignment, where a variable is set to contain a value:

$word = 'hello';
$number = 5;
// etc.

echo "I said $word $number times!";

Regarding the increment opperators:

You'll see things like ++$x and $i-- as you learn PHP (and/or other languages). These are increment/decrement operators. Where they're positioned in relation to the variable they're operating on is important.

If they're placed before the variable, like ++$x, it's a pre-increment/decrement. This means the operation is performed before anything else can be done to the variable. If it's placed after, like $x++, it's a post-increment/decrement, and it means that the operation is performed afterward.

It's easiest to see in an example script:

$x = 5;

echo ++$x; // 6
echo $x++; // ALSO 6
echo $x; // NOW 7
Saturday, August 7, 2021
 
EurekA
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :