Return Statement in PHP : Tutorial


Return statement are used to terminate the execution of a function and return control to the calling function.
Return statement can also return a value to the calling function.
Return is a language construct, it is not a function, hence parentheses are not required to surround the arguments.
If no arguments are provided NULL is returned.


Example 1:
<?php

$num = 22;

function evenOddTest($no){

	if($no%2 ==0) {
		return "$no is even";
	}

	else {
		return "$no is odd";
	}
}

echo evenOddTest($num);

?>
Output

22 is even
Example 2:
<?php

function nothingToDo(){

	echo "This function returns noting, hence var_dump returns : ";
	
	}

//var_dump() returns NULL if function has no return!
echo var_dump(nothingToDo());

?>
Output

This function returns noting, hence var_dump returns : NULL