PHP - Function Function return

Introduction

You can have as many return statements in your function.

PHP will exit the function as soon as it finds one.

Demo

<?php
    function aFunction($a) { 
        if ($a == 7) { 
            return "You are seven"; 
        } else { 
            return "You are not ."; 
        } //from  ww  w . j  a  v a2  s .co  m
     } 
     print(aFunction(1));
     print(aFunction(7));
?>

Result

Additionally, you can omit the return statement if you do not want the function to return anything.

In this case, the function will end once it reaches the end of the block of code.

Related Topics