Fatal error: Uncaught Error: Call to undefined function addIt() in /var/www/html/php8/ch5/variables.php:9 Stack trace: #0 {main} thrown in /var/www/html/php8/ch5/variables.php on line 9

 $result =  addIt(12,13);
 echo $result;

    It would appear that you haven't defined your addIt() function.

      Or you defined it with a different name?

        function addIt(12,13);
        $result = addIt(12,13);
        echo $result;
        Parse error: syntax error, unexpected integer "12", expecting variable in /var/www/html/php8/ch5/variables.php on line 8

          And the elephant in the room: why do you need a function to add two numbers when you can just use the + operator? Assuming it's some sort of learning exercise (and using a reasonably recent version of PHP), you could build in some error-checking with:

          function addIt(int|float $a, int|float $b): int|float {
            return $a + $b;
          }
          
          $result = addIt(12,13);
          echo $result;
          

            Perhaps you need to read and do the code examples in chapters 1-4.

            maxxd
            I did the chapters 1-4
            thank you NogDog

             function addIt(int $a, int $b): int {
                return $a + $b;
              }
             $result =  addIt(12,13);
             echo $result;
            
             echo "<br>";
             function addIt1(float $a, float $b): float {
                return $a + $b;
              }
             $result1 =  addIt1(12.1,13.3);
             echo $result1;
            
             echo "<br>";
            
             function addIt2(int $a, string $b): int {
                return $a + $b;
              }
              $result2 =  addIt2(12,"13");
              echo $result2;

            I do not understand the right : int
            function addIt(int $a, int $b): int

            bertrc I do not understand the right : int

            Basically that is announcing the PHP type of whatever will be returned by the function. Just like the use of int, float, etc. in front of the variable names in the argument list, which are saying "you must provide this type for this argument", the ): int { is saying, in this case, "the only type of thing I will return is an integer."

              Write a Reply...