Jump to content

RETURN Ends The Script Flow Regardless If Result Is TRUE/FALSE?


phpsane

Recommended Posts

Folks,

 

Is it true that once the RETURN shows you a result, be it TRUE or FALSE, the script gets executed no further in the script flow ?

Did I understand it correctly here:

http://php.net/manual/en/functions.returning-values.php

 

I had 2 scripts (2b & 2a) in the same file like so ...

My questions are in the comments. If anyone knows the answers then kindly respond.

 

Thanks

<?php
 
/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
 
 
//script 2b:
 
$banned_words = array("0","1","2","3","4","5","6","7","8","9");
$content = "4";
 
    foreach ($banned_words as $ban) {
        if (stripos($content, $ban) !== FALSE) {
            echo "Script 2b - Match: $ban";?><br><?php
            return true; //QUESTION 1: What is the mystery behind the "return true/false" ? Why the script does not flow below this point to the rest of the script plus to the other script script 2a ?
        }
    }
        echo "Script 2b - No Match: $ban";?><br><?php
    return false; //What is the mystery behind the "return false" ?
 
//Showing result: Script 2b - Match: 4.
//QUESTION 2: Why is it not showing the other matches & non-matches ? Why showing only the first match ?
 
?>
<br>
 
<?php
 
//script 2a: 
 
$banned_words = array("0","1","2","3","4","5","6","7","8","9");
$content = "f"; //Tailored for no match to be found.
 
    foreach ($banned_words as $ban) {
        if (stripos($content, $ban) !== FALSE) {
            echo "Script 2a - Match: $ban";?><br><?php
            return true; 
        }
    }
        echo "Script 2a - No Match: $ban";?><br><?php
    return false; 
 
?>
<br>
 

Link to comment
Share on other sites

You can use RETURN outside of a function, but it is not common. I will use it in included() files to return execution to the calling script to avoid having to add complicated if/then, switch or other structures. However, I don't believe using return in an included script will return any value.

 

From the manual for RETURN:

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.

 

If called from the global scope, then execution of the current script file is ended.

 

Your code above would fall into the latter.

 

And, yes, it does not matter if the return value is True, False, a string, and array, etc. A return is a return.

Link to comment
Share on other sites

However, I don't believe using return in an included script will return any value.

It will, you can use that as hackish way to make a config file or something for example.

<?php return [
  'foo' => 'bar'
];
<?php
$config = include('config.php');
var_dump($config);
Link to comment
Share on other sites

So, after the RETURN, there is no need for an EXIT or a BREAK or anything else ?

The script would get the function to return a value and then get halted ? Right ?

(Mmm. Why do I get the feeling this is not the case and if I need to EXIT or BREAK then I need to include them as the RETURN would only add the value of the function to the concerned variable and still continue the flow in the script flow ? Or, am I assuming too much ?).

Anyway, Let's get this answered asap so I can close the thread as BEST ANSWER as other pressing threads are still waiting to be seen to.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.