hI @cxr, thanks for your feedback.
Maybe my question is incomplete....
And if array as compose by words and not numbers like:
$arr = array(million, sea, peace cake, rock roll, something);
$number = peace;
Regards, JC
Main Topics
Browse All TopicsHi E's, In snippet code I put a example of a foreach. That will output:
All sequence is 1
Sequence without number 1
All sequence is 2
Sequence without number 2
All sequence is 3
Sequence without number 3
All sequence is 4
Sequence without number 4
What I want to do is, if the value of $arr is = $number the system don't print this line:
echo "Sequence without number $number";
And if $number = 3, the input will be:
All sequence is 1
Sequence without number 1
All sequence is 2
Sequence without number 2
All sequence is 3
All sequence is 4
Sequence without number 4
How I check if $number is = $arr, don't print the echo, and continue to number four or more.....
Regards, JC
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
It works the exact same way, but remember to use apostrophes or quotes for constant strings in PHP:
$arr = array('million', 'sea', 'peace cake', 'rock roll', 'something');
$number = 'peace';
In this case, 'peace' will not be found, because it is not one of the items in the array ('peace cake' is a different value).
If you want to find a substring within a string, you can use the strpos() function:
if(substr($value,$number)!
http://php.net/strpos
Because that function does not return the boolean value "true". It returns an integer: the position of the $number within the $value. In this test case, it would return 0 (zero), because "peace" is at the beginning of the string "peace cake". (0==true) is false, it would not find it. However, (0!== false) is true. Note the double ==, needed in this case because (0!=false) is false, and (0==false) is true. This is because PHP has a loose type system, integer 0 is evaluated as boolean false when using == or != operators and comparing with a boolean value. !== and === requires that both operands are of the same type to be considered equal.
Hi @cxr, I try to do this solution in my real code and don't work. Maybe the problem are apostrophes or quotes.
The array I use to do the foreach give me this output:
Array
(
[0] => banco
[1] => world
[2] => dw
[3] => lista
[4] => download
[5] => digital
[6] => dvd
[7] => wallpaper
[8] => dinheiro
[9] => brasil
[10] => video
)
Explain My code:
The objective of my code is made a cycle in array with foreach, and save each word and scores in database.
The variable $palavradb is the variable that contain the word or words to check if exist inside array, if exists the script interrupt and don't save in database, and continue in foreach, if not, the script continue. $palavradb is the variable that I use inside strpos: if(strpos($nomek,$palavrad
Because I have two kinds of scores, one for $palavra = 1 word, and other for $palavradb => 2 words, I use $n_palavras_b and this variable contain the size of $palavradb (the first line in snippet code).
$keysrel_numeracao contain the array with the score and $keysrel_nomeinverte contain the word
I will check if word exists in database, if exists the script update, if not exists the script insert.
===================
I try use strpos but don't work, maybe you can help and tell me where I put the function strpos in my two foreach ($n_palavras_b = 1 or $n_palavras_b = 2 or more words inside $palavradb).
So, how I interrupt the foreach when the system discover equal words?
Note: interrupt just that foreach, the foreach continue for the next word!
Regards, JC
Hi, JC. I am not sure I understand your issues using foreach or looking for a substring, but you have an error in your use of MySQL. You are using mysql_query() without testing for success. Here is the correct way to do that. You should update your code to test the queries for success and to print diagnostic information on failure.
Also, take note of the indenting used at the control structures in the code snippet above. Note that the curly braces like { and } line up under each other. This will make your code MUCH more readable.
Perhaps you can simplify your question. If so, please post back here and I will try to help with a succinct answer. But at this point, I think cxr showed you the basis of how to handle this challenge.
Best regards, ~Ray
>> I try to do this solution in my real code and don't work.
Your code does not contain any occurences of strpos(), and I don't see where it was supposed to be used. Variable names and comments in a language I don't understand does not make it easier... could you please point me to the correct place in the source code?
>> So, how I interrupt the foreach when the system discover equal words?
In general, use the "continue;" keyword. This will stop the execution of the current loop and continue the foreach with the next item in the array.
Thanks @ray for yours posts!
@cxr, I'm sorry my bad english, I try to explain better then I can.
I try to put strpos example in lines 13 and 72, but the function don't work. Why don't work? because the system will just save in database the word that I want that not be save in database (is contrary to the desired).
I use strpos in this way:
if(strpos($nomek,$pala
} //close the cycle of line 13 and 72 in lines 58 and 102.
Basecly what I want is, if the script found equal words between $nomek and $palavradb (like: batata - batata OR world - the great world), the foreach don't progresses for that value and will check the next value.
So, the problem now is why the script just save in db the word that I don't want, like:
If $palavradb = poker and $nomek was poker they save and read all script, but if $nomek = cards the system don't work. In this example the system should save cards and not poker.
This is the only problem when I use strpos.
Can help me with srtpos or other solution?
Regards, JC
I have inserted the strpos() condition the two places you described. I also fixed the indentation, it makes it easier to read. That is the only changes I have done in the snippet below.
>> the system should save cards and not poker.
Are you saying it is working the oppsite way of what you want? If so, just change the !== to === like this:
if(strpos($nomek,$palavrad
Hi @cxr, continues don't work, including change to === false.
I want to know where is the error, so I put more this two lines to view the contain of variables:
$lif = strpos($nomek,$palavradb);
echo "<br>$lif ---- $nomek ---- $palavradb<br>"; //line 15
If I use '!==false', the script save in database the word that I don't want to save, and give me this output (consider $palavradb = bolacha):
0 ---- bolacha ---- bolacha
If i use '===false' the system don't save anything.
If I erase the line 13 and 59, and continue with this lines:
$lif = strpos($nomek,$palavradb);
echo "<br>$lif ---- $nomek ---- $palavradb<br>";
and consider $palavradb = paixao, the output is:
0 ---- paixao ---- paixao
---- dia ---- paixao
---- bodas ---- paixao
---- genero ---- paixao
---- amor ---- paixao
---- musical ---- paixao
So, maybe the problem was the boolean. I don't know, but is very strange. Maybe what you talk some posts ago about apostrophes or quotes are influence in this case.
Regards, JC
>> continues don't work
In this case you have this structure:
foreach(...) {
# some code
if(...) {
# save to database
}
}
If you were to use "continue;", you would use it like this:
foreach(...) {
# some code
if(...) continue;
# save to database
}
Note that one set of {} is removed.
>> If i use '===false' the system don't save anything.
That was odd. It should save those words that was NOT saved when you used '!==false'. "===" is the opposite of "!==", the ! means "not".
>> and consider $palavradb = paixao, the output is:
>> 0 ---- paixao ---- paixao
>> ---- dia ---- paixao
For the first word, "paixao", the value of $lif is 0, and it is !==false. For the second word, "dia", the value of $lif is false (which makes no output, it won't print 'false') and it should be saved if you used ===false.
When you are comparing single words, you should not use strpos(), you should use just: if($nomek!=$palavradb). It is slightly faster, and it makes the code easier to understand. Never use more complicated code than what is needed.
<<When you are comparing single words, you should not use strpos(), you should use just: if($nomek!=$palavradb). It is slightly faster, and it makes the code easier to understand. Never use more complicated code than what is needed.>>
Maybe was the solution for singles words, and if it is for more then 1 word?
Regards, JC
>> if it is for more then 1 word?
Then you can use strpos(), as shown above in http:#a24773671
Note that it will also find partial words, for instance "peace" will be found in "so peacefull" and "ample" will be found in "example". If you need to match full words only, use explode() and in_array():
$words = 'my example';
$wordlist = explode(' ',$words);
if(in_array('ample',$wordl
if(in_array('example',$wor
In this example, "ample" would not be found, but "example" would be found.
Using explode() and in_array() also works if the $words string contains just a single word.
Business Accounts
Answer for Membership
by: cxrPosted on 2009-07-03 at 09:49:13ID: 24773408
Use the expression ($value!=$number), like this:
Select allOpen in new window