简体   繁体   中英

Variable containing a string is not echoing anything

I have a function that converts an array into an array with rows of html code to be printed.

One of my html rows is only created if a particular string contains the number 1. Var_dump shows the string does in fact contain 1. The row of html then concatenates the string onto a bunch of spaces (for tidy formatting of the html).

However when I echo this html row the string is not there. If I replace the string in my row of html with the character 1 then it echoes properly.

If I var_dump the string inside an if statement checking if the string contains "1" then it gives me the result string(1) "1", which is what I expect.

The string in question:

$item['qty']

Code in question:

if ($item['qty'] == '1') {
    $html[] = '<div class="qty">'. $item['qty'] .'</div>';
}

Results in nothing being echoed. If I use the following code then I see the character 1 in the place I expect:

if ($item['qty'] == '1') {
    $html[] = '<div class="qty">1</div>';
}

And using var_dump gives the following result:

if ($item['qty'] == '1') {
    var_dump($item['qty']);
    exit();
}
string(1) "1"

My string clearly contains the value 1. It clearly is accepted by the if statement. All my other strings echo absolutely fine.

Edit:

Simplified the reproduction of the problem, so if I use the following line:

$html[] = '            <div class="qty">'.$item['sta'].'</div>';

I get the following in my source:

<div class="qty">0</div>

This is expected. var_dump of $item['sta']:

string(1) "0"

If I change the code to:

$html[] = '            <div class="qty">'.$item['qty'].'</div>';

I get the following source:

<div class="qty"></div>

var_dump of $item['qty']:

string(1) "1"

Edit 2:

The following block of code seems to be removing the $item['qty'] value from my html:

function prepareItems ($itemArray,$containerSize,$page,$table) {
    $num = 0;
    foreach ($itemArray as $item) {
        for ($i = 1; $i <= 4; $i++) {
            for ($i2 = 1; $i2 <= $containerSize; $i2++) {
                $preparedItems[$i][$i2] = $this->itemToHtml($page,$table,$num,$item);
                if ($num == 0) { var_dump(htmlspecialchars($preparedItems['1']['1']['3'])); }
                $num++;
            }
        }
    }
    var_dump(htmlspecialchars($preparedItems['1']['1']['3']));exit();
    return $preparedItems;
}

This gives the following var_dump output:

string(58) " <div class="qty">1</div>" string(57) " <div class="qty"></div>"

If I add a break in all of my loops then I get the correct var_dump output:

function prepareItems ($itemArray,$containerSize,$page,$table) {
    $num = 0;
    foreach ($itemArray as $item) {
        for ($i = 1; $i <= 4; $i++) {
            for ($i2 = 1; $i2 <= $containerSize; $i2++) {
                $preparedItems[$i][$i2] = $this->itemToHtml($page,$table,$num,$item);
                if ($num == 0) { var_dump(htmlspecialchars($preparedItems['1']['1']['3'])); }
                $num++;
                break;
            }
            break;
        }
        break;
    }
    var_dump(htmlspecialchars($preparedItems['1']['1']['3']));exit();
    return $preparedItems;
}

var_dump:

string(58) " <div class="qty">1</div>" string(58) " <div class="qty">1</div>"

What about this function is removing the value I need?

Solved. I needed to remove the foreach line as it was messing up what was being given to my itemToHtml function:

function prepareItems ($itemArray,$containerSize,$page,$table) {
    $num = 0;
    for ($i = 1; $i <= 4; $i++) {
        for ($i2 = 1; $i2 <= $containerSize; $i2++) {
            $preparedItems[$i][$i2] = $this->itemToHtml($page,$table,$num,$itemArray[$num]);
            if ($num == 0) { var_dump(htmlspecialchars($preparedItems['1']['1']['3'])); }
            $num++;
        }
    }
    var_dump(htmlspecialchars($preparedItems['1']['1']['3']));exit();
    return $preparedItems;
}

The correct var_dump:

string(58) " <div class="qty">1</div>" string(58) " <div class="qty">1</div>"

Thanks to yunzen giving me a tip of where to look.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM