Skip to content

Kirby 4.1.2

$page->increment()

Increment a given field value

$page->increment(string $field, int $by = 1, ?int $max = null): Kirby\Cms\Page

Parameters

Name Type Default
$field * string
$by int 1
$max int|null null

Return type

Kirby\Cms\Page

This method does not modify the existing $page object but returns a new object with the changes applied. Learn more →

Parent class

Kirby\Cms\Page inherited from Kirby\Cms\ModelWithContent

Example

Kirby's objects are immutable. That means, when you modify an object like $page, $file etc. using a method like update(), changeTitle() and so on, a new object is returned. Therefore, you have to store the returned object in a new variable to be able to further work with it.

<?php $page->increment('likes') ?>

Notice

The increment method will only work reliably with field values that PHP can parse into a valid integer. Otherwise the field value will be evaluated as zero and incremented to 1 or whatever $by value you pass.