1111出现在打印序列之后-php

1111出现在打印序列之后-php,php,Php,早上好 我有一些代码,需要一个鞋码串,分解它,然后把它放在一个表中。代码工作正常,信息正确显示在页面上,但在表格后面总是有几个1。我真的很困惑 因此,该表如下所示(在网页上的三列中) 有以下尺寸可供选择: 3 4 5 6910 111 下面是代码,用于示例$x=3 | 4 | 6 | 10 | 9 function get_sizes_pipe($x) { $counter = 0; $splits = explode("|",$x); asort($

早上好

我有一些代码,需要一个鞋码串,分解它,然后把它放在一个表中。代码工作正常,信息正确显示在页面上,但在表格后面总是有几个1。我真的很困惑

因此,该表如下所示(在网页上的三列中)

有以下尺寸可供选择: 3 4 5 6910

111

下面是代码,用于示例$x=3 | 4 | 6 | 10 | 9

function get_sizes_pipe($x) {

$counter = 0;
$splits = explode("|",$x);
asort($splits);

$x = print "<table class='greentable'>";

$x .= print "<thead><tr><th scope='col' colspan='3' abbr='Starter'>Available in the following sizes:</th></tr></thead><tbody><tr>";

foreach ($splits as $split) {
    $entry = print "<td>".$split."</td>";

    if($counter == 2){
        $entry .= print "</tr><tr>";
        $counter =0;
    } else {

    $counter++;

    }

}

if($counter == 1){
    $entry .= print "<td></td><td></td>";
}elseif ($counter == 2) {
    $entry .= print "<td></td>";
}

$x.= $entry;

$x.= print "</tr></tbody></table>";

return $x;

}
函数获取管道尺寸($x){
$counter=0;
$splits=explode(“|”,$x);
asort($splits);
$x=打印“”;
$x.=打印“有以下尺寸:”;
foreach($拆分为$拆分){
$entry=打印“$split.”;
如果($counter==2){
$entry.=打印“”;
$counter=0;
}否则{
$counter++;
}
}
如果($counter==1){
$entry.=打印“”;
}elseif($counter==2){
$entry.=打印“”;
}
$x.=$entry;
$x.=打印“”;
返回$x;
}
我真的很感激任何帮助

谢谢
Chris

我不知道为什么要将
print
与某个变量连接起来,但您应该知道函数
print
作为执行的结果返回
1
(始终如此)

$x .= print 'some value';

print 'some value';
$x .= 1;

尝试以下操作:不要使用打印,因为您已经将其附加到
$x
中,只需
echo$x

 <?php
function get_sizes_pipe($x) {

$counter = 0;
$splits = explode("|",$x);
asort($splits);

$x =  "<table class='greentable'>";

$x .= "<thead><tr><th scope='col' colspan='3' abbr='Starter'>Available in the following sizes:</th></tr></thead><tbody><tr>";

foreach ($splits as $split) {
    $entry .= "<td>".$split."</td>";//use . to concate here

    if($counter == 2){
        $entry .= "</tr><tr>";
        $counter =0;
    } else {

    $counter++;

    }

}

if($counter == 1){
    $entry .="<td></td><td></td>";
}elseif ($counter == 2) {
    $entry .=  "<td></td>";
}

$x.= $entry;

$x.="</tr></tbody></table>";

echo $x;

}

get_sizes_pipe("1|2|3|4");

不要使用字符串连接(使用大字符串会变得效率低下),而是将html字符串添加到数组中,并从函数中返回展开的html

function get_sizes_pipe($x) {
    $counter = 0;
    $splits = explode("|",$x);
    asort( $splits );

    $html=array();

    $html[]="
        <table class='greentable'>
            <thead>
                <tr>
                    <th scope='col' colspan='3' abbr='Starter'>Available in the following sizes:</th>
                </tr>
            </thead>
        <tbody>
            <tr>";

    foreach( $splits as $split ) {
        $html[]="<td>".$split."</td>";
        if( $counter == 2 ){
            $html[]="</tr><tr>";
            $counter=0;
        } else {
            $counter++;
        }

    }

    $html[]=( $counter == 1 ) ? "<td></td><td></td>" : "<td></td>";
    $html[]="
            </tr>
        </tbody>
    </table>";

    return implode( PHP_EOL, $html );
}

/* call your function with whatever value of `$x` you need */
echo get_sizes_pipe( $what_is_x );
函数获取管道尺寸($x){
$counter=0;
$splits=explode(“|”,$x);
asort($splits);
$html=array();
$html[]=”
有以下尺寸可供选择:
";
foreach($拆分为$拆分){
$html[]=“.$split.”;
如果($counter==2){
$html[]=“”;
$counter=0;
}否则{
$counter++;
}
}
$html[]=($counter==1)?“”:“”;
$html[]=”
";
返回内爆(PHP_EOL,$html);
}
/*使用所需的“$x”值调用函数*/
echo获取管道尺寸($what_is_x);

print
函数返回一些值。不要使用print,因为您已经将其附加到$x,只需回显$x;明亮的工作得很好!为什么要浓缩第一个条目?我只是给出了可以相应更改的想法。现在您知道print return true/1,所以最好使用echo来显示字符串。我是php自学的,我知道它与打印有关,但我不知道它总是返回值,感谢您的知识!