如何连接变量php / html?

时间:2017-03-28 17:54:55

标签: php concatenation

这是我的代码:

<?php
$image = ''; 
if (getUserChar($_SESSION['user_login']) == 1) {
    $image = 'warrior';
    echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
}
elseif (getUserChar($_SESSION['user_login']) == 2) {
    $image = 'mage';
    echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
}
elseif (getUserChar($_SESSION['user_login']) == 3) {
    $image = 'archer';
    echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
}
?>

有了这个,这个班似乎工作得很好。但关于变量top_pos - 它没有。

1 个答案:

答案 0 :(得分:0)

我怀疑你的$ top_pos和$ left_pos变量没有被设置或被覆盖。

<?php

function getUserChar($input) {
    return $input;
}

// I suspect you have not set these anywhere or are being over written somewhere
$top_pos                = 123;
$left_pos               = 321;
$image                  = ''; 
$_SESSION['user_login'] = 1;

$input = getUserChar($_SESSION['user_login']);

if ($input == 1) {
    $image = 'warrior';
} elseif ($input == 2) {
    $image = 'mage';
} elseif ($input == 3) {
    $image = 'archer';
}

echo '<div class="'.$image.'" style="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';

http://sandbox.onlinephpfunctions.com/code/184e6935fa39e733c145441c6a24be6651f41c25

相关问题