1

I am having a strange problem with defining and accessing array values. The strings I've created return empty. When I do a var_dump, I see that the array values have the correct string lengths, but result in "". When I attempt to access the variable, I receive "". Can someone explain this behavior?

//define the case combinations
$cases = array(
    "<CO><VO><CO><VOP>",
    "<VO><CLOSED><VO>",
    "<OPEN><CO><VO>",
    "<CO><VOP><ESCB>",
    "<COP><VO><CO><CVN>",
    "<STVE><ESCB>"
);

The var dump:

array(6) { [0]=> string(17) "" [1]=> string(16) "" [2]=> string(14) "" [3]=> string(15) "" [4]=> string(18) "" [5]=> string(12) "" }

A similar problem happens with other arrays:

$siteSuffix = array(
"data" => array('com','org','net','edu'),
"probabilities" => array(1,1,1,1), //not actually used
"name" => "siteSuffix"

);

The array under 'data' prints strings in the same manner.

If the problem is external of this, here is a github link where the rest of the code is: link

8
  • Both of the codes are working completely fine for me, the issue might be with something else. Commented Nov 1, 2017 at 3:59
  • I'm going to guess that it is some sort of character set issue. What is the charset of your script? Commented Nov 1, 2017 at 4:00
  • I also want to point out that the var_dump is showing that these are not empty strings, when it prints out the string length. Commented Nov 1, 2017 at 4:01
  • What externally could cause this? These are both arrays that are not modified once created. Here's the small github repo if you want to take a look link Commented Nov 1, 2017 at 4:01
  • Also, for the other comments, I'm using the default charset (I did not specify). Also, this does not integrate with any html/css, it is pure PHP. Lastly, when I compare var_dump examples online, they actually explicitly print the string, as well as the length, whereas mine only prints the length. Commented Nov 1, 2017 at 4:03

2 Answers 2

2

It looks like because you have htmlentities in your actual data (array) < and > PHP will not interpret these as actual values/strings.

What you can do is convert the characters using htmlspecialchars(), for example:

$cases = array(
    "<CO><VO><CO><VOP>",
    "<VO><CLOSED><VO>",
    "<OPEN><CO><VO>",
    "<CO><VOP><ESCB>",
    "<COP><VO><CO><CVN>",
    "<STVE><ESCB>"
);

foreach ($cases as $case){
   var_dump(htmlspecialchars($case));
}

Output of var_dump:

string(41) "<CO><VO><CO><VOP>" string(34) "<VO><CLOSED><VO>" string(32) "<OPEN><CO><VO>" string(33) "<CO><VOP><ESCB>" string(42) "<COP><VO><CO><CVN>" string(24) "<STVE><ESCB>"
Sign up to request clarification or add additional context in comments.

1 Comment

Turns out, that's right! When I use a straight jquery test, I get what I'm expecting!
0

The problem is with the angle brackets < and > to print them in browser you can simply use echo htmlentities($str) or htmlspecialchars($str)for each array item. If you are using php command line, then you don't need such encoding.

Refer: https://www.w3schools.com/php/func_string_htmlentities.asp

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.