0

I have a problem with my script over here. I have got this array:

Array
(
    [KUNDENNUMMER] => 
    [BEZ] => 
    [DATUM] => 2014-10-10
    [VON] => 11:10:36
    [BIS] => 11:48:11
    [TAETIGKEIT] => Berufschule
    [BEZ_01] => 
    [DAUER] => 0001-01-01 00:37:00
    [STUNDEN] => 0.61
    [VERRECHENBAR] => F
    [BEMERKUNG] => 0x000c5cf2000000ba
    [USER_BEZ] => Armani, Kia
    [TZ_BEZ] => 
    [TT_VERRECHENBAR] => F
    [TT_ID] => 80
)

I want to echo "Cake" when $row (the array) [TAETIGKEIT] == Berufschule using this code

if(strpos($row['TAETIGKEIT'], 'Berufschule') === true) echo "Cake";

But the echo never gets called. I also tried to compare directly

if($row['TAETIGKEIT'] == 'Berufschule') echo "Cake";

but it did not work either. When I do

print_r($row['TAETIGKEIT'];

it prints

Berufschule

What am I doing wrong?

11
  • 2
    What does var_dump($row['TAETIGKEIT'], urlencode($row['TAETIGKEIT'])); output? Commented Oct 13, 2014 at 13:24
  • 2
    Maybe the value has preceding spaces? Try if(strpos(trim($row['TAETIGKEIT']), 'Berufschule') !== false) echo "Cake"; Commented Oct 13, 2014 at 13:26
  • 1
    @KiaArmani and if you had used var_dump you would have seen spaces in the string if any, regardless strpos would still have found the string so it doesn't really make sense that using trim worked as the latter parameter is what it would search for. Commented Oct 13, 2014 at 13:29
  • 2
    @Paul The second parameter is the word to search for (it goes haystack, needle). See this demo for why it would not make a difference. Commented Oct 13, 2014 at 13:31
  • 1
    That's the mean thing about PHP, I always get mixed up with the parameter order since it's not standardized throughout the language. You are absolutely correct. Commented Oct 13, 2014 at 13:32

2 Answers 2

1

For summary, code in question didn't work for various reasons presumably.

I can only assume, that the value of TAETIGKEIT has either a trailing or preceding space.

if(strpos($row['TAETIGKEIT'], 'Berufschule') === true) echo "Cake";
// doesn't work since strpos returns an integer if string is found or `false` if not.
// it never returns true

if($row['TAETIGKEIT'] == 'Berufschule') echo "Cake";
// doesn't work due to the superfluous space, thus it's not exactly the same

Solutions would be to use strpos correctly

if(strpos($row['TAETIGKEIT'], 'Berufschule') !== false) echo "Cake";

Or trim the values before comparison

if(trim($row['TAETIGKEIT']) == 'Berufschule') echo "Cake";

And as noted by h2ooooooo, var_dump would show these addtional spaces.

Sign up to request clarification or add additional context in comments.

Comments

0

strpos never returns true - it returns false or integer value, so with === true you will never get pass.

Just try with:

strpos($row['TAETIGKEIT'], 'Berufschule') !== false

3 Comments

@KiaArmani Your main problem is that strpos does not mean "bool isSubstringInString". It returns the index position of your match if it's found. Not true. !== false will only match if the substring was found.
if(strpos($row['TAETIGKEIT'], 'Berufschule') !== false) echo cake; does not print cake for me.
I think is an example where type coercion is actually the only solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.