0

I am storing data from a form to user data in Concrete5.

I am pulling the user data successfully:

$u = new User();
$ui = UserInfo::getByID($u->getUserID());
$testtype = $ui->getAttribute('TestType','display');

This return what I expect. But when I try to evaluate it using...

} else if ($testtype == "English Adult Male") {

...it doesn't trigger.

I echo the output I am pulling, and notice the html put a <br> after the echo of the variable. I have tried to pass the output through trim but the same comes out.

What am I doing wrong?

James Korden
  • 724
  • 4
  • 19

2 Answers2

1

use === instead of == for comparison in general as a good practice. More read on that here

Change your code this way that you have $case with string in it and do var_dump of both $case and $testtype

<?php

$testtype = "English Adult Male ";
$case = "English Adult Male ";
var_dump($testtype);
var_dump($case);

if ($testtype === "whatever") {
    echo "IF!";

} else if ($testtype === $case) {
    echo "ELSE IF!";
}

the code above generates output:

string(19) "English Adult Male "
string(19) "English Adult Male "
ELSE IF!

Note

string(19) not string(23)

you have 23 in your var dump that is more than number of character in the string "English Adult Male " that leads me to the conclusion that you have a multibyte character encoding but probably you are testing with your string that is not multibyte but singlebyte and its 19 bytes string vs 23 bytes string.

you can convert your $testtype by mb_convert_encoding()

and its supported encodings

also mb_detect_encoding may be handy to detect encoding of $testtype;

Jimmix
  • 5,644
  • 6
  • 44
  • 71
  • Both the test and the variable are returning an ASCII type. There are, I assume, hidden/encoding characters. I assume the 2 extra would be for `\n` or `\r` but trim should have taken care of that, right? – James Korden Jun 03 '19 at 00:22
  • is both `var_dump()` = string(**23**) "English Adult Male "? Yes [trim()](https://www.php.net/manual/en/function.trim.php) should remove `\r` and `\n` for new lines, as well as spaces. You may also use [mb_strtolower](https://www.php.net/manual/en/function.mb-strtolower.php) to make case insensitive comparison. – Jimmix Jun 03 '19 at 00:39
  • Trim still returns a 23 length. Maybe this is a concrete5 specific thing. I will check on their forums! – James Korden Jun 03 '19 at 00:57
0

The issue was with the line

$testtype = $ui->getAttribute('TestType','display');

By changing this to:

$testtype = $ui->getAttribute('TestType');

Then setting the conditional to ==, because === wouldn't work, the result was as desired.

Hope this helps someone!

James Korden
  • 724
  • 4
  • 19