0

Here is my function:

function search_title_and_vendor{
    if (stripos($title, 'Tsugi') !== false) {
        if (stripos($vendor, 'Puma') !== false) {
            return 'Puma Tsugi';
        } else {
            return '';
        }
    }   
}

Where the variables are:

$title = 'Puma Tsugi'
$vendor = 'Puma'

As you can see I tried to nest the if statement to search for two variables, if they match, return 'Puma Tsugi'. However, this returns nothing.

In my file, I also have occurrences with, for example, Vans Tsugi, where $vendor = 'Vans'; and $title = 'Vans Tsugi sneakers'.

How can I search for a combination like this and, return a given value?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
MantiNL
  • 69
  • 12
  • declare them global at the beginning of your function – Milan Markovic Jul 18 '17 at 22:07
  • @MilanMarkovic global is never a good answer; far better to pass the values as arguments to the function – Mark Baker Jul 18 '17 at 22:09
  • Yes, but it references the real source of the problem. Arguments should be used but that's another lesson for him :) – Milan Markovic Jul 18 '17 at 22:15
  • Read about [PHP functions](http://php.net/manual/en/language.functions.php). – axiac Jul 18 '17 at 22:15
  • There are many things to correct with your code. PHP will expected `()` after `search_title_and_vendor`. If the first condition does not pass, there is no `return` for it. Why don't you combine the two conditionals into one statement? If the two values need to be in certain positions relative to each other in the string, then you might use regex or compare the stripos values using `>` or `<`. Please update your question to better represent the goal of your code considering a few different input samples. – mickmackusa Jul 18 '17 at 23:22

1 Answers1

0

You should pass your info into the function using parameters -- like so:

$search_result = search_title_and_vendor('Puma Tsugi','Puma');

Then your original function should contain the variable declarations in the parentheses:

function search_title_and_vendor($title, $vendor){

So the full function should look like:

function search_title_and_vendor($title, $vendor){
if (stripos($title, 'Tsugi') !== false) {
    if (stripos($vendor, 'Puma') !== false) {
        return 'Puma Tsugi';
    }
    else {return '';}
}   
}

Then $search_result should contain the desired result:

echo $search_result;
Zak
  • 6,976
  • 2
  • 26
  • 48