Raspberry Pi_Eng_24.4.7 Stop and Return of Processing


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


24.4.7  Stop and Return of Processing

 

24.4.7.1    "exit" Statement and "die" Statement

 

"exit" statement is a command that prints the specified message and terminates the execution of the PHP script. "die" statement is the same command as "exit" statement.

 

exit  ( [  status-message ]  )

exit  ( [  status-number ]  )

 

If there is no status message, it can be used without parentheses.

 

Running this command will stop running of the script. Even if "exit" is executed, shutdown operation of the function or destructor operation of the object is still executed.

 

[Used Example]

Here is an example of "exit" statement

 

<?php

 

//exit program normally

exit;

exit();

exit(0);

 

//exit with an error code

exit(1);

exit(0376); //octal

 

?>

 


 

24.4.7.2    "return" Statement

 

"return" statement returns program execution flow to the parent calling program, and continues processing after the statement that called the sub program.

 

return   [ argument ]

 

If "return" statement is executed within a function, execution of the function is aborted and the argument specified as the result of the function processing is returned. "return" statement also terminates execution of "eva ( )" statement or script file.

 

If "return" is executed in the topmost script file with global scope, execution of the current script file will be aborted. If the current script file is "included" or "required" in another script file, control passes back to the calling script file. Furthermore, if the current script file is "included" elsewhere, the value specified in "return" command will be passed as the result of calling "include". If "return" is executed in the main script file, the script stops executing.

 

"return" and "exit" perform a similar function, but there is difference in that "return" stops execution of the current script, and "exit" stops execution of the entire script.

 

[Used Example]

Here is an example of "return" statement.

 

<?php

function sum($x, $y) {

    $z = $x + $y;

    return $z;

}

 

echo "5 + 10 = " . sum(5, 10) . "<br>";

echo "7 + 13 = " . sum(7, 13) . "<br>";

echo "2 + 4 = " . sum(2, 4);

?>

 

Below is an example of using "return" statement in an "included" script.

 

// script file -- a.php

//(executing a.php:) will echo "ba".
<?php
include("b.php");
echo "a";
?>

// script file -- b.php
<?php
echo "b";
return;
?>

 

Below is an example of using "exit" statement in an "included" script.

 

// script file -- a.php

//(executing a.php:) will echo "b".
<?php
include("b.php");
echo "a";
?>

// script file -- b.php
<?php
echo "b";
exit;
?>