Jump to content

Why RETURN FALSE Only Shows Last Non-Match And Not The Previous Ones ?


phpsane

Recommended Posts

Php Gurus,

 

 

Why is it that the RETURN FALSE only echos the last non-matched item ?

I had like this:

$banned_words = array("0","1","2","3","4","5","6","7","8","9");
$content = "f"; //Tailored for no match to be found.
 
As you can see, the script is tailored not to find any matches.
The RETURN FALSE does not show you result like this:
 
Script 2a - No Match: 0
Script 2a - No Match: 1
Script 2a - No Match: 2
Script 2a - No Match: 3
Script 2a - No Match: 4
Script 2a - No Match: 5
Script 2a - No Match: 6
Script 2a - No Match: 7
Script 2a - No Match: 8
Script 2a - No Match: 8
 
But only shows you like this:
Script 2a - No Match: 9
 
 
<?php
 
//script 2a: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string
 
$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; //What is the mystery behind the "return true" ?
        }
    }
        echo "Script 2a - No Match: $ban";?><br><?php
    return false; //What is the mystery behind the "return false" ?
 
//Showing result: No match: 9. 
//Why is it not showing the other non-matches ? Why showing only the last non-match ?
 
?>
<br>
Link to comment
Share on other sites

.....continued from original post as this forum keeps deleting the below content:

 
 
Q1. Why does it only mention about the last item in the array (banned words list), in this case about "9" only ? Why not about the other earlier 8 items ?
Q2. How to sort this out so it also shows "No Match:" for the other 8 too ? How to get the RETURN to show about the other 8 (be it using RETURN FALSE or RETURN TRUE or BOTH) ? I know how to get it to show results regarding the other 8 without using the RETURN and now from you I want to learn to get the RETURN to show the results of the other 8.
 
This is how I get the script to show results about the other 8 items (be they were matched or non-matched) without using the RETURN:
 

<?php
 
$banned_words = array("0","1","2","3","4","5","6","7","8","9");
$content = "0,1,3,5,7,9";
 
foreach ($banned_words as $ban) {
    if (stripos($content,$ban) > -1){
        echo "Script 3 - Match: $ban";?><br><?php
    }else{
        echo "Script 3 - No Match: $ban";?><br><?php
    }
}
 
?>

Link to comment
Share on other sites

A1. Because that line is outside the loop.

A2. For output, make the line be inside the loop. You cannot return more than one value so what you're asking is impossible, but you can return one array that contains true/false values for each match made or not made.

Link to comment
Share on other sites

I really hope this is a homework problem used to understand some basic commands and logic flow. If not, you just need to stop because there is way too much wrong with this code.

 

I would HIGHLY suggest you add comments to your code. For one, it is the right thing to do. Plus, writing comments to explain what the code is supposed to do will help you avoid easy mistakes. if you write good comments some of these things will be apparent. If you were to have added good comments to your first code, the results you are getting would have been apparent.

 

 

//Create an array of banned words
$banned_words = array("0","1","2","3","4","5","6","7","8","9");
//Define content to check for banned words
$content = "f"; //Tailored for no match to be found.
 
//Iterate over each banned word
foreach ($banned_words as $ban) {
    //Check to see if the banned word exists in the content
    if (stripos($content, $ban) !== FALSE) {
        //If the banned word DOES exist, use RETURN to exit current execution
        return true; //What is the mystery behind the "return true" ?
    }
    //AFTER looping through all banned words - echo a Mo Match statement
    // *** This makes no sense since this line would always get executed (even if there were banned words)
    echo "Script 2a - No Match: $ban";
    //Use RETURN to exit execution
    return false; //What is the mystery behind the "return false" ?
}
Link to comment
Share on other sites

A1. Because that line is outside the loop.

A2. For output, make the line be inside the loop. You cannot return more than one value so what you're asking is impossible, but you can return one array that contains true/false values for each match made or not made.

 

Thanks. I got my answers:

 

1. More than one "no match" is not being shown because the line is outside the loop.

2. Not more than one value can be returned.

3. Can return one array that contains true/false values for each match/non-matches.

(If you don't mind, how-about a code snippet so we don't have to rely on buggy codes from stackoverflow anymore ?

https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

I'm sure other newbies would find it useful. I can then close this thread as solved by you). ;)

 

PS - This forum keeps cutting my posts in half (anything after a code sample) and making me repeat the last past of my posts. Therefore, erased the code sample to avoid the erasure again.

Link to comment
Share on other sites

Why all the extra "<?php" tags?

 

When you need to write html in the middle of php code (in this case br) then you need to write the php closing tag and then write the html. When you finished writing the html then you need to write the php opening tag again to continue writing the php again. Else, you see errors. If you have any doubt then check for yourself. Try this in your xammp/wamp and see what error you get:

<?php
 
//script 2a: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string
 
$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>
            return true; //What is the mystery behind the "return true" ?
        }
    }
        echo "Script 2a - No Match: $ban";?><br>
    return false; //What is the mystery behind the "return false" ?
 
//Showing result: No match: 9. 
//Why is it not showing the other non-matches ? Why showing only the last non-match ?
 
?>
<br>
Link to comment
Share on other sites

Why are you even doing this in general? Banned words from what? Throw up asterisks for the big seven or whatever and fucking move on.

 

EDIT: I threw fucking in there to see what this forum did, btw.

 

I grabbed the code samples from here:

https://stackoverflow.com/questions/13795789/check-if-string-contains-word-in-array

https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

 

Have a look at all the bad coding practices. I was not aware they were bad until I brought the samples to this forum and you guys started complaining how bad the codes are. ;)

Link to comment
Share on other sites

 

I really hope this is a homework problem used to understand some basic commands and logic flow. If not, you just need to stop because there is way too much wrong with this code.

 

I would HIGHLY suggest you add comments to your code. For one, it is the right thing to do. Plus, writing comments to explain what the code is supposed to do will help you avoid easy mistakes. if you write good comments some of these things will be apparent. If you were to have added good comments to your first code, the results you are getting would have been apparent.

//Create an array of banned words
$banned_words = array("0","1","2","3","4","5","6","7","8","9");
//Define content to check for banned words
$content = "f"; //Tailored for no match to be found.
 
//Iterate over each banned word
foreach ($banned_words as $ban) {
    //Check to see if the banned word exists in the content
    if (stripos($content, $ban) !== FALSE) {
        //If the banned word DOES exist, use RETURN to exit current execution
        return true; //What is the mystery behind the "return true" ?
    }
    //AFTER looping through all banned words - echo a Mo Match statement
    // *** This makes no sense since this line would always get executed (even if there were banned words)
    echo "Script 2a - No Match: $ban";
    //Use RETURN to exit execution
    return false; //What is the mystery behind the "return false" ?
}

 

Comments were missing from my codes because they are not really my codes.

No comments existed here:

https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

See post 2.

I just edited a little, like variable names.

Link to comment
Share on other sites

 

When you need to write html in the middle of php code (in this case br) then you need to write the php closing tag and then write the html. When you finished writing the html then you need to write the php opening tag again to continue writing the php again. Else, you see errors. If you have any doubt then check for yourself. Try this in your xammp/wamp and see what error you get:

 

Absolutely not true. You should either use echo statements to output content when in code or (even better) store content in variables to output in the presentation layer. Breaking in and out of tags makes your code messy and error prone.

 

For example this:

 

        if (stripos($content, $ban) !== FALSE) {
            echo "Script 2a - Match: $ban";?><br><?php //Bad process
            return true;
        }

Why break out of PHP to output a <BR> tag? The preceeding statement is an echo - just put the BR tag in there like this:

 

        if (stripos($content, $ban) !== FALSE) {
            echo "Script 2a - Match: $ban<br>"; //FIXED
            return true;
        }
Link to comment
Share on other sites

 

Absolutely not true. You should either use echo statements to output content when in code or (even better) store content in variables to output in the presentation layer. Breaking in and out of tags makes your code messy and error prone.

 

For example this:

        if (stripos($content, $ban) !== FALSE) {
            echo "Script 2a - Match: $ban";?><br><?php //Bad process
            return true;
        }

Why break out of PHP to output a <BR> tag? The preceeding statement is an echo - just put the BR tag in there like this:

        if (stripos($content, $ban) !== FALSE) {
            echo "Script 2a - Match: $ban<br>"; //FIXED
            return true;
        }

 

 

Thanks Psycho,

 

This works:

 
$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>";
            return true; //What is the mystery behind the "return true" ?
        }
    }
 
 
//Showing result: No match: 9. 
//Why is it not showing the other non-matches ? Why showing only the last non-match ?
 
?>
 
Link to comment
Share on other sites

... 2nd part of the message continued from above post as forum erased the bottom half of my message again after the code. Now, having to repeat the writing !!!

 

Psycho,

 

If you put the br html tag outside the double quote, like the following, then you see error:

 

Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\e_id\filter.php on line 20

$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>";
            return true; //What is the mystery behind the "return true" ?
        }
    }
 
?>
Link to comment
Share on other sites

... 3rd part of the message continued from above post (2nd part of the message) as forum AGAIN erased the bottom half of my message after the 2nd code. Now, having to repeat the writing again for the 3rd time !!!

 

Psycho,

 

Yes, I know you would say, why do I not just include the br html tag inside the double quote ?

Yes, I can do this from now on. Before you told me, I was not aware of it. I thought the br html tag has to be outside the double quote. And putting it outside the double quote showed error. And so, I had to resort to adding the extra php opening & closing tags.
I think I have answered ginerjm's question.

Thanks for bringing it to my attention that the extra php opening & closing tags are not necessary if you add the br html tag inside the double quote. :)

And thanks to ginerjm for bring up the subject that lead me to give my reason which in return lead you to correct my mistaking assumption. ;)

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.