Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 55 additions & 24 deletions concepts/comparison-operators/about.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,66 @@
# Comparison Operators

PHP has ten built in comparison operators:

| Name | Example | Result |
| ----- | ---------- | -------------------------------------------------- |
| Equal | `$a == $b` | true if `$a` is equal to `$b` after type juggling. |
| Identical | `$a === $b` | true if `$a` is equal to `$b` and the same type. |
| Not Equal | `$a != $b` | true if `$a` is not equal to `$b` after type juggling. |
| Not Equal | `$a <> $b` | true if `$a` is not equal to `$b` after type juggling. |
| Not identical | `$a !== $b` | true if `$a` is not equal to `$b` or not of the same type. |
| Less than | `$a < $b` | true if `$a` is strictly less than `$b`. |
| Greater than | `$a > $b` | true if `$a` is strictly greater than `$b`. |
| Less than or equal to | `$a <= $b` | true if `$a` is less than or equal to `$b`. |
| Greater than or equal to | `$a >= $b` | true if `$a` is greater than or equal to `$b`. |
| Spaceship | `$a <=> $b` | returns an integer less than, equal to, or greater than `0`i when `$a`. |

PHP has distinct definitions that differentiate between `equal` and `identical`.
Sometimes `identical` is also referred to as `strictly equal`.
Comparison operators compare two values.
Most of them return a boolean (`true` or `false`).
The spaceship operator (`<=>`) is different: it returns `-1`, `0`, or `1`.

PHP has ten built-in comparison operators:

| Name | Example | Result |
| --- | --- | --- |
| Equal | `$a == $b` | `true` if `$a` is equal to `$b` after type juggling |
| Identical | `$a === $b` | `true` if `$a` is equal to `$b` and the same type |
| Not Equal | `$a != $b` | `true` if `$a` is not equal to `$b` after type juggling |
| Not Equal | `$a <> $b` | `true` if `$a` is not equal to `$b` after type juggling |
| Not identical | `$a !== $b` | `true` if `$a` is not equal to `$b` or not the same type |
| Less than | `$a < $b` | `true` if `$a` is strictly less than `$b` |
| Greater than | `$a > $b` | `true` if `$a` is strictly greater than `$b` |
| Less than or equal to | `$a <= $b` | `true` if `$a` is less than or equal to `$b` |
| Greater than or equal to | `$a >= $b` | `true` if `$a` is greater than or equal to `$b` |
| Spaceship | `$a <=> $b` | `-1`, `0`, or `1` when `$a` is less than, equal to, or greater than `$b` |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While in most situations -1, 0 and 1 are the result, PHP only defines An int less than, equal to, or greater than zero in the documentation. I think we should stick with the official documentation wording here, not stating something that might not always hold true.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I’ll update this table row to match the PHP docs wording.

Same idea also shows up in a few other place, worth updating those too for consistency?

  • about.md L5 (intro sentence)
  • about.md L56–63 (spaceship section)
  • introduction.md L32–34 (the -1 / 0 / 1 list)

For the code examples, I’d leave the -1/0/1 results as they are. What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjust about.md L5 (intro sentence) and introduction.md L32–34 (the -1 / 0 / 1 list), but not about.md L56–63 (spaceship section) or introduction.md L39–41 which are both provable concrete examples known to yield -1 / 1.

I think that's the distinguishing thing: Is it proven to be -1 / 1 or hypothetical?


## Equal and identical

PHP distinguishes between **equal** (`==`) and **identical** (`===`).
Identical is also called **strictly equal**.

With `==`, PHP may change the type of a value before comparing (type juggling).
With `===`, both the value and the type must match.

```php
<?php

// Comparisons between integer and numeric string values
1 == "1"; // => true, equal
1 == "1"; // => true, equal after implicit conversion of string to int
1 === "1"; // => false, not identical

// Comparisons between integers and floating point values
1 == 1.0; // => true
1 == 1.0; // => true, equal after implicit conversion of int to float
1 === 1.0; // => false
```

The same idea applies to "not equal" (`!=` / `<>`) versus "not identical" (`!==`).

## Comparing objects

For objects, `==` checks whether properties are equal.
`===` checks whether both sides refer to the same instance.

```php
<?php

new stdClass() == new stdClass(); // => true
new stdClass() === new stdClass(); // => false
```

## The spaceship operator

// Comparisons between object instances
new stdClass() == new stdClass(); // => true, properties are equal
new stdClass() === new stdClass(); // => false, references are not identical
`$a <=> $b` returns:

```php
<?php

1 <=> 2; // => -1
1 <=> 1; // => 0
2 <=> 1; // => 1
```

This is useful when you need an ordering result, not only `true` or `false`.
75 changes: 49 additions & 26 deletions concepts/comparison-operators/introduction.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
# Comparison Operators

PHP has ten built in comparison operators:

| Name | Example | Result |
| ----- | ---------- | -------------------------------------------------- |
| Equal | `$a == $b` | true if `$a` is equal to `$b` after type juggling. |
| Identical | `$a === $b` | true if `$a` is equal to `$b` and the same type. |
| Not Equal | `$a != $b` | true if `$a` is not equal to `$b` after type juggling. |
| Not Equal | `$a <> $b` | true if `$a` is not equal to `$b` after type juggling. |
| Not identical | `$a !== $b` | true if `$a` is not equal to `$b` or not of the same type. |
| Less than | `$a < $b` | true if `$a` is strictly less than `$b`. |
| Greater than | `$a > $b` | true if `$a` is strictly greater than `$b`. |
| Less than or equal to | `$a <= $b` | true if `$a` is less than or equal to `$b`. |
| Greater than or equal to | `$a >= $b` | true if `$a` is greater than or equal to `$b`. |
| Spaceship | `$a <=> $b` | returns an integer less than, equal to, or greater than `0`i when `$a`. |

PHP has distinct definitions that differentiate between `equal` and `identical`.
Sometimes `identical` is also referred to as `strictly equal`.
Comparison operators compare two values and usually return a boolean (`true` or `false`).
They are commonly used to make decisions in code.

PHP has **identical** comparisons and relational comparisons between numbers:

```php
<?php

// Comparisons between integer and numeric string values
1 == "1"; // => true, equal
1 === "1"; // => false, not identical
5 === 5; // => true
5 !== 0; // => true
3 < 7; // => true
9 > 2; // => true
5 <= 5; // => true
4 >= 8; // => false
```

| Operator | Meaning |
| --- | --- |
| `$a === $b` | identical: equal and the same type |
| `$a !== $b` | not identical: different type or not equal |
| `$a < $b` | less than |
| `$a > $b` | greater than |
| `$a <= $b` | less than or equal to |
| `$a >= $b` | greater than or equal to |

## The spaceship operator

// Comparisons between integers and floating point values
1 == 1.0; // => true
1 === 1.0; // => false
The spaceship operator (`<=>`) also compares two values, but it returns an integer instead of a boolean:

- `-1` when the left value is less than the right value
- `0` when both values are equal
- `1` when the left value is greater than the right value

```php
<?php

// Comparisons between object instances
new stdClass() == new stdClass(); // => true, properties are equal
new stdClass() === new stdClass(); // => false, references are not identical
3 <=> 7; // => -1
5 <=> 5; // => 0
9 <=> 2; // => 1
```

## Using comparisons in an `if` statement

A comparison can be used as the condition of an `if` statement.
If the comparison is `true`, the code inside the braces runs:

```php
<?php

if ($value === 10) {
return 0.77;
}

if ($value >= 5) {
return 0.9;
}
```
Comment on lines +44 to +59

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole part should move to the exercise.

Suggested change
## Using comparisons in an `if` statement
A comparison can be used as the condition of an `if` statement.
If the comparison is `true`, the code inside the braces runs:
```php
<?php
if ($value === 10) {
return 0.77;
}
if ($value >= 5) {
return 0.9;
}
```

14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@
],
"status": "beta"
},
{
"slug": "cars-assemble",
"name": "Cars, Assemble!",
"uuid": "4949e5b9-c685-4380-9d70-46ae970035cf",
"concepts": [
"comparison-operators"
],
"prerequisites": [
"booleans",
"integers",
"floating-point-numbers"
],
"status": "beta"
},
{
"slug": "windowing-system",
"name": "Windowing System",
Expand Down
29 changes: 29 additions & 0 deletions exercises/concept/cars-assemble/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Hints

## General

- The introduction covers the comparison operators you need for this exercise.
- You may also review the [comparison operators][comparison-operators] documentation.

## 1. Calculate the success rate

- Compare `$speed` with the boundary values from the table.
- Use `identity`, `greater than`, `greater or equal`, `lower than`, `lower or equal` operators as needed.
- You can use an `if` statement to return the matching success rate.

## 2. Calculate the production rate per hour

- Reuse the `successRate()` method you wrote earlier: `$this->successRate($speed)`.
- Multiply `221`, `$speed`, and the success rate.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, that is described very good in the instructions.

Suggested change
- Multiply `221`, `$speed`, and the success rate.

- PHP can multiply integers and floating-point numbers together; the result may be a float.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- PHP can multiply integers and floating-point numbers together; the result may be a float.
- PHP can multiply integers and floating-point numbers together.


## 3. Check whether the line is running

- Use a not-identical comparison (`!==`) against `0`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Use a not-identical comparison (`!==`) against `0`.
- Use the not-identical comparison from the list of comparison operators.

- Return the boolean result of that comparison.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Return the boolean result of that comparison.
- Directly return the result of that comparison.


## 4. Compare two line speeds

- The spaceship operator (`<=>`) returns `-1`, `0`, or `1` directly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- The spaceship operator (`<=>`) returns `-1`, `0`, or `1` directly.
- There is a comparison operator that returns `-1`, `0`, or `1`.
- Directly return the result of that comparison.


[comparison-operators]: https://www.php.net/manual/en/language.operators.comparison.php
82 changes: 82 additions & 0 deletions exercises/concept/cars-assemble/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Instructions

In this exercise you'll write code to analyze the production of an assembly line in a car factory.
The assembly line's speed can range from `0` (off) to `10` (maximum).

At its lowest speed (`1`), `221` cars are produced each hour.
The production increases linearly with the speed.
So with the speed set to `4`, the line should produce `4 * 221 = 884` cars per hour.
However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded.

You have four tasks.

## 1. Calculate the success rate

Implement the `successRate()` method to calculate the ratio of cars created without error for a given speed.
Use comparisons to choose the correct rate from this table:

- `0`: 0% success rate (`0.0`)
- `1` to `4`: 100% success rate (`1.0`)
- `5` to `8`: 90% success rate (`0.9`)
- `9`: 80% success rate (`0.8`)
- `10`: 77% success rate (`0.77`)

```php
<?php

$assemblyLine = new CarsAssemble();
$assemblyLine->successRate(10);
// => 0.77
```

## 2. Calculate the production rate per hour

Implement the `productionRatePerHour()` method to calculate how many cars are successfully produced per hour.
Multiply the base production (`221` cars per hour at speed `1`) by the speed and by the success rate for that speed.

```php
<?php

$assemblyLine = new CarsAssemble();
$assemblyLine->productionRatePerHour(6);
// => 1193.4
```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I read through it again: Where is the comparison part in this task 2? Maybe, the production rate should not be calculated, when the speed is over the line's maximum (> 10) or running backwards (< 0)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now task 2 doesn’t introduce a new comparison on purpose: it’s meant to reuse successRate() and multiply (221 * $speed * successRate). The comparison practice sits in tasks 1, 3, and 4.

Adding a guard for speeds < 0 or > 10 would give task 2 a clear comparison moment. Something like:

## 2. Calculate the production rate per hour
Implement the `productionRatePerHour()` method to calculate how many cars are successfully produced per hour.
Multiply the base production (`221` cars per hour at speed `1`) by the speed and by the success rate for that speed.
If the speed is below `0` or above the line's maximum (`10`), do not calculate production — return `0.0`.
<?php
$assemblyLine = new CarsAssemble();
$assemblyLine->productionRatePerHour(6);
// => 1193.4
$assemblyLine->productionRatePerHour(-1);
// => 0.0
$assemblyLine->productionRatePerHour(11);
// => 0.0

Do you think we should go that way, or leave task 2 as reuse + arithmetic?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add the comparisons. But use -1.0 as the production rate for invalid input: 0.0 * X === 0.0 anyways (the success rate for <= 0 is 0.0).

## 3. Check whether the line is running

Implement the `isLineRunning()` method to return whether the assembly line is running.
The line is running when its speed is **not identical** to the `off` speed (`0`).

```php
<?php

$assemblyLine = new CarsAssemble();
$assemblyLine->isLineRunning(0);
// => false

$assemblyLine->isLineRunning(3);
// => true
```

## 4. Compare two line speeds

Implement the `compareSpeeds()` method to compare two speeds.
It should return:

- `-1` when the first speed is less than the second
- `0` when both speeds are equal
- `1` when the first speed is greater than the second

```php
<?php

$assemblyLine = new CarsAssemble();
$assemblyLine->compareSpeeds(3, 7);
// => -1

$assemblyLine->compareSpeeds(5, 5);
// => 0

$assemblyLine->compareSpeeds(9, 2);
// => 1
```
61 changes: 61 additions & 0 deletions exercises/concept/cars-assemble/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Introduction

## Comparison Operators

Comparison operators compare two values and usually return a boolean (`true` or `false`).
They are commonly used to make decisions in code.

For learning PHP, start with **identical** comparisons and relational comparisons between numbers:

```php
<?php

5 === 5; // => true
5 !== 0; // => true
3 < 7; // => true
9 > 2; // => true
5 <= 5; // => true
4 >= 8; // => false
```

| Operator | Meaning |
| --- | --- |
| `$a === $b` | identical: equal and the same type |
| `$a !== $b` | not identical |
| `$a < $b` | less than |
| `$a > $b` | greater than |
| `$a <= $b` | less than or equal to |
| `$a >= $b` | greater than or equal to |

### The spaceship operator

The spaceship operator (`<=>`) also compares two values, but it returns an integer instead of a boolean:

- `-1` when the left value is less than the right value
- `0` when both values are equal
- `1` when the left value is greater than the right value

```php
<?php

3 <=> 7; // => -1
5 <=> 5; // => 0
9 <=> 2; // => 1
```

### Using comparisons in an `if` statement

A comparison can be used as the condition of an `if` statement.
If the comparison is `true`, the code inside the braces runs:

```php
<?php

if ($value === 10) {
return 0.77;
}

if ($value >= 5) {
return 0.9;
}
```
3 changes: 3 additions & 0 deletions exercises/concept/cars-assemble/.docs/introduction.md.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction

%{concept:comparison-operators}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
%{concept:comparison-operators}
%{concept:comparison-operators}
## Using comparisons in an `if` statement
A comparison can be used as the condition of an `if` statement.
If the comparison is `true`, the code inside the braces runs:
```php
<?php
if ($value === 10) {
return 0.77;
}
if ($value >= 5) {
return 0.9;
}
```

21 changes: 21 additions & 0 deletions exercises/concept/cars-assemble/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"pablo-miralles"
],
"files": {
"solution": [
"CarsAssemble.php"
],
"test": [
"CarsAssembleTest.php"
],
"exemplar": [
".meta/exemplar.php"
]
},
"forked_from": [
"csharp/cars-assemble"
],
"icon": "cars-assemble",
"blurb": "Learn about comparison operators by analyzing a car assembly line!"
Comment thread
pablo-miralles marked this conversation as resolved.
}
Loading