0
<?php

$x = 4;
$y = 5;

    function greaterOrSmaller($x,$y){
        if($x>$y){
            return 1;
        }elseif ($x==$y) {
            return 0;
        }else{
            return -1;
        }
    }
    
    if(greaterOrSmaller($x,$y)==1){
        echo"{$x} is greater than {$y}";
    }elseif(greaterOrSmaller($x,$y)==0){
        echo"{$x} is equal to {$y}";
    }else(greaterOrSmaller($x,$y)==-1){
        echo"{$x} is smaller than {$y}";
    }
CBroe
  • 91,630
  • 14
  • 92
  • 150
Babri Wala
  • 3
  • 1
  • 2
  • 4
    `else(...) { ... }` does not make sense. This needs to either be a plain `else { ... }`, if you have no more conditions to check at this point - or it needs to be another `elseif`. – CBroe Jan 31 '22 at 13:59
  • By the way, if you are using PHP >= 7, the "spaceship" operator is an in-built function which does the same as your `greaterOrSmaller()` function: `$comparison = $x <=> $y;` - https://www.php.net/manual/en/language.operators.comparison.php – Simon K Jan 31 '22 at 14:11

1 Answers1

0

You forgot the in your last comparison the if in the elseif expression

Try this :

<?php


$x = 4;
$y = 5;

    function greaterOrSmaller($x,$y){
        if($x>$y){
            return 1;
        }elseif ($x==$y) {
            return 0;
        }else{
            return -1;
        }
    }
    
    if(greaterOrSmaller($x,$y)==1){
        echo"{$x} is greater than {$y}";
    }elseif(greaterOrSmaller($x,$y)==0){
        echo"{$x} is equal to {$y}";
    }elseif(greaterOrSmaller($x,$y)==-1){
        echo"{$x} is smaller than {$y}";
    }
Gwentey
  • 34
  • 1
  • 7