Björn C
Björn C

Reputation: 4008

Decrement character with php

Why is it that it's ok to increment character but not decrement with PHP?

PHP

<?php
    $a = "a";
    echo $a. "<br>";  //a
    echo ++$a. "<br>";  //b
    echo --$a. "<br>";  //b
>

Is there a simple way as --$ato decrement a charrater?

There was a solution by using chr.

Upvotes: 13

Views: 5361

Answers (6)

Masandy Hariyanto
Masandy Hariyanto

Reputation: 95

you can use this.

function stringDecrementer($string)
{
    $len = strlen($string);
    if ($len == 1) {
        if (strcasecmp($string,"A") == 0) {
            return "A";
        }
        return chr(ord($string) - 1);
    } else {
        $s = substr($string, -1);
        if (strcasecmp($s, "A") == 0) {
            $s = substr($string, -2, 1);
            if (strcasecmp($s, "A") == 0) {
                $s = "Z";
            } else {
                $s = chr(ord($s) - 1);
            }
            $output = substr($string, 0, $len - 2) . $s;
            if (strlen($output) != $len && $string != "AA") {
                $output .= "Z";
            }
            return $output;
        } else {
            $output = substr($string, 0, $len - 1) . chr(ord($s) - 1);
            return $output;
        }
    }
}

Upvotes: -1

Pedro Lobito
Pedro Lobito

Reputation: 98921

As chris85 mentioned: "Character variables can be incremented but not decremented"


PHP supports C-style pre- and post-increment and decrement operators.

Incrementing/Decrementing Operators

  1. ++$a Pre-increment Increments $a by one, then returns $a.
  2. $a++ Post-increment Returns $a, then increments $a by one.
  3. --$a Pre-decrement Decrements $a by one, then returns $a.
  4. $a-- Post-decrement Returns $a, then decrements $a by one.

Note: The increment/decrement operators only affect numbers and strings. Arrays, objects and resources are not affected. Decrementing NULL values has no effect too, but incrementing them results in 1.


SRC: http://php.net/manual/en/language.operators.increment.php

Upvotes: 1

Gaurav Mahajan
Gaurav Mahajan

Reputation: 290

Please try with this. Output is a b a.

$a = "a";
echo $a. "<br>";  
echo $next = chr(ord($a) + 1). "<br>";
echo $prev = chr(ord($next) - 1 ). "<br>"; 

Upvotes: 2

D Coder
D Coder

Reputation: 572

Simple function you can achieve it:

function decrementChar($Alphabet) {
return chr(ord($Alphabet) - 1);
}

Upvotes: 0

Ghulam Ali
Ghulam Ali

Reputation: 1935

There is no direct way to decrement alphabets. But with a simple function you can achieve it:

function decrementLetter($Alphabet) {
    return chr(ord($Alphabet) - 1);
}

Source, thanks to Ryan O'Hara

Upvotes: 15

Mark Baker
Mark Baker

Reputation: 212412

There is no simple way, especially if you start with multi-character strings like 'AA'.

As far as I can ascertain, the PHP Internals team couldn't decide what to do when

$x = 'A';
$x--;

so they simply decided not to bother implementing the character decrementor logic

Upvotes: 6

Related Questions