是否有任何方式样式Php变量与CSS


Is there any way to Style Php variable with CSS?

是否有办法用CSS样式表来样式化Php变量?下面是php变量:

if(empty($email)){
    $msg[] = "Email address required.";
    $msg['error'] = true;
}

我想样式这个Php变量$msg[]颜色为红色。这可能吗?如果可以,怎么做呢?

是的,我可以用

设置这个错误信息的样式
<font color='red'>....</font> 

或使用

<div class='error'>

但是我有太多的验证在我的形式,所以我正在寻找一种方法来颜色的Php变量。

认为我得到了你想要的;

// Set an array to hold all your errors
$messages = [];
// Define what inputs to validate
$fields_to_check = ['email', 'username', 'password'];
// Loop through and check for empty values
foreach($fields_to_check as $field)
{
    // Fill $messages with errors if validation failed.
    if (!isset($_POST[$field]) || empty($_POST[$field]))
    {
        $messages[$field] = ucfirst($field).' is required!';
    }
}
// If $messages is not empty, there are errors.
if (!empty($messages))
{
    $html = '<ul>';
    foreach($messages as $input => $message)
    {
        $html .= '<li class="red">'.$message.'</li>';
    }
    $html .= '</ul>';
    return $html;
}

$msg的输出将显示为红色…

if(empty($email)){
    $msg[] = '<span style="color:red">Email address required.</span>';
    $msg['error'] = true;
}