Reputation: 23
I'm trying to print using array matches strings...
The problem is... it prints only BTC print.
And if possible, also suggest a simply way to match using an array parameter.
Output Needed:
BTC print
ETH print
DOGE print
WAVES print
Code:
<?php
$coins = array("BTC", "ETH", "DOGE", "WAVES");
foreach ($coins as $coin) {
$string = 'BTC';
if (strpos($string, $coin) !== FALSE) {
echo "BTC print";
return true;
}
$string1 = 'ETH';
if (strpos($string1, $coin) !== FALSE) {
echo "ETH print";
return true;
}
$string2 = 'DOGE';
if (strpos($string2, $coin) !== FALSE) {
echo "DOGE print";
return true;
}
$string3 = 'WAVES';
if (strpos($string3, $coin) !== FALSE) {
echo "WAVES print";
return true;
}
}
echo "Not found!";
return false;
Upvotes: 0
Views: 389
Reputation: 19780
Returns will break your loop. Try this:
$coins = array("BTC", "ETH", "DOGE", "WAVES");
$found = false;
foreach ($coins as $coin) {
$string = 'BTC';
if (strpos($string, $coin) !== FALSE) {
echo "BTC print";
$found = true;
}
$string1 = 'ETH';
if (strpos($string1, $coin) !== FALSE) {
echo "ETH print";
$found = true;
}
$string2 = 'DOGE';
if (strpos($string2, $coin) !== FALSE) {
echo "DOGE print";
$found = true;
}
$string3 = 'WAVES';
if (strpos($string3, $coin) !== FALSE) {
echo "WAVES print";
$found = true;
}
}
if (!$found) {
echo "Not found!";
}
If you are in a function, then you could add:
return $found;
Upvotes: 2