0

So I am having a similar issue to this post: PHP strpos not working, but not quite.

Here is my situation (from a CodeIgniter application):

$_submit = strtolower($this->input->post('form-submit'));

if(strpos('save', $_submit) !== FALSE){
    // we have to save our post data to the db
}
if(strpos('next'), $_submit) !== FALSE){
    // we have to get the next record from the db
}

The problem is, neither of these actually fire, despite form-submit containing one, or both of those values. The values form-submit receives are: 'save', 'save-next', and 'skip-next' (which I have confirmed by looking at the post data as it comes in). Now for the real head scratcher, I also have this line in the same code chunk:

if ($_submit === 'add-comment'){
    //do something
}

And that works perfectly fine. So === is working as expected, but !== is not?

Community
  • 1
  • 1
Joel Kinzel
  • 969
  • 2
  • 7
  • 19

3 Answers3

4

You are giving arguments wrong to the strpos function.......

$submit = strtolower($this->input->post('form-submit'));

if(strpos($submit,'save') !== FALSE){
    // we have to save our post data to the db
}
if(strpos($submit,'next') !== FALSE){
    // we have to get the next record from the db
}

Please look into the strpos function in php.net manual....first argument is full string and second one is key string

You can find a small example here also.

Venkata Krishna
  • 4,287
  • 6
  • 30
  • 53
2

Your arguments to strpos are the wrong way round: as the manual states it is strpos ( string $haystack , mixed $needle ). In your code you are looking for the needle $_submit in the haystack 'save'.

So if(strpos($_submit, 'save') !== FALSE)

[Of course, when testing 'save' against 'save', either way around will work, which is probably what confused you.]

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • Thanks, I feel like a noob. Apparently I can't read all that well. – Joel Kinzel Feb 19 '13 at 18:09
  • Heh, we've all done it. Particularly with PHP, which has a tendency not to be very consistent in its argument order... – IMSoP Feb 19 '13 at 20:59
  • Yeah I noticed that. I was like "I know it's needle then haystack" and then I looked again, and this time it's haystack/needle. AGH! – Joel Kinzel Feb 20 '13 at 01:52
1

I would recommend to use switch instead of multiple if conditions.

$_submit = strtolower($this->input->post('form-submit'));

switch($_submit) {
    case 'save':
    case 'save-comment': // example for different spelling but same action...
         // we have to save our post data to the db
    break;
    case 'next':
         // we have to get the next record from the db
    break;
    case 'add-comment':
         // we have to save our post data to the db
    break;
    default:
        die('Unknown parameter value');
    break;
} 
powtac
  • 40,542
  • 28
  • 115
  • 170
  • Usually I would, time is short so I'm just doing the quickest solution for now. When I go back and re-factor, then I will break stuff out more. Thanks for the input! – Joel Kinzel Feb 19 '13 at 18:09