phpsane Posted August 29, 2017 Share Posted August 29, 2017 (edited) 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> Edited August 29, 2017 by phpsane Quote Link to comment https://forums.phpfreaks.com/topic/304757-return-ends-the-script-flow-regardless-if-result-is-truefalse/ Share on other sites More sharing options...
requinix Posted August 29, 2017 Share Posted August 29, 2017 All of your "questions" are answered by the documentation. Except for maybe why it's using return outside of a function, but you're probably the only one who can answer that. Quote Link to comment https://forums.phpfreaks.com/topic/304757-return-ends-the-script-flow-regardless-if-result-is-truefalse/#findComment-1550314 Share on other sites More sharing options...
Sepodati Posted August 29, 2017 Share Posted August 29, 2017 Why do you even have "return" in there? what are you doing? Quote Link to comment https://forums.phpfreaks.com/topic/304757-return-ends-the-script-flow-regardless-if-result-is-truefalse/#findComment-1550332 Share on other sites More sharing options...
Psycho Posted August 29, 2017 Share Posted August 29, 2017 (edited) 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. Edited August 29, 2017 by Psycho 1 Quote Link to comment https://forums.phpfreaks.com/topic/304757-return-ends-the-script-flow-regardless-if-result-is-truefalse/#findComment-1550334 Share on other sites More sharing options...
kicken Posted August 29, 2017 Share Posted August 29, 2017 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); Quote Link to comment https://forums.phpfreaks.com/topic/304757-return-ends-the-script-flow-regardless-if-result-is-truefalse/#findComment-1550342 Share on other sites More sharing options...
phpsane Posted September 2, 2017 Author Share Posted September 2, 2017 (edited) 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. Edited September 2, 2017 by phpsane Quote Link to comment https://forums.phpfreaks.com/topic/304757-return-ends-the-script-flow-regardless-if-result-is-truefalse/#findComment-1550609 Share on other sites More sharing options...
Solution Sepodati Posted September 2, 2017 Solution Share Posted September 2, 2017 return also ends the execution of an eval() statement or script file. How is that confusing to you? 2 Quote Link to comment https://forums.phpfreaks.com/topic/304757-return-ends-the-script-flow-regardless-if-result-is-truefalse/#findComment-1550613 Share on other sites More sharing options...
phpsane Posted September 2, 2017 Author Share Posted September 2, 2017 (edited) How is that confusing to you? Ok. I'm choosing your answer as the BEST ANSWER and closing this thread. Thanks for the manual's reference! Edited September 2, 2017 by phpsane Quote Link to comment https://forums.phpfreaks.com/topic/304757-return-ends-the-script-flow-regardless-if-result-is-truefalse/#findComment-1550620 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.