无法将 if 语句与 dompdf 一起使用
Not able to use if statement with dompdf
这是我的代码:
<?php
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$root/config.php";
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$stmt = $pdo->prepare('SELECT name FROM my_table');
$stmt->execute();
$results = $stmt->fetchAll();
foreach( $results as $row ) {
$dompdf = new DOMPDF();
$html = "
".if(!empty($row['name'])) {
echo "My name is".$row['name']."";
}."
";
$dompdf->load_html($html);
}
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream();
?>
我总是得到:
Parse error: syntax error, unexpected 'if' (T_IF) in
/var/www/user_name/html/create_pdf.php on line 17
像这样的简单 HTML 代码 $html = "<p>Hello, it's me!</p>";
有效。
也 PHP 这样的代码 $html = "My name is ".$row['name']."!";
有效。
只是 if 语句似乎不起作用。
我做错了什么?
$html = "text... " . (!empty($row['...'])) ? $row['...'] : " " . " more text...";
删除 if 并创建一个 'inline' 或上面的三元表达式。
If 语句不能串联,除非使用 shorthand 表达式(即:三元)
对于您的情况,这应该有效:
$html = " " . (!empty($row['name'])) ? "Your name is " . $row['name'] : "" . " ";
对于更长的连接,你可以这样做(和可读性):
$html = "";
$html .= (expression) ? True : False;
$html .= "";
为了连接,我们在 PHP 中使用 .=
。
我建议在 PHP 中长期使用 print_f()
方法 HTML,例如。
print_f("your name is %s", (!empty($row['name'])) ? $row['name'] : 'Default');
你不能在中间用 if
连接字符串(因为 if
语句没有 return 任何东西)。
您可以使用三元运算符解决此问题:
$html = "
".(!empty($row['name']) ? "My name is".$row['name'] : '')."
";
如果你有很长的 if-else 语句,你应该关闭字符串并在里面连接它:
$html = "";
$html .= "Some string\n";
if (....) {
$html .= "Something new\n";
} elseif (...) {
$html .= "more...\n";
} else {
$html .= "...\n";
}
$html .= "Some text\";
这是我的代码:
<?php
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$root/config.php";
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$stmt = $pdo->prepare('SELECT name FROM my_table');
$stmt->execute();
$results = $stmt->fetchAll();
foreach( $results as $row ) {
$dompdf = new DOMPDF();
$html = "
".if(!empty($row['name'])) {
echo "My name is".$row['name']."";
}."
";
$dompdf->load_html($html);
}
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream();
?>
我总是得到:
Parse error: syntax error, unexpected 'if' (T_IF) in /var/www/user_name/html/create_pdf.php on line 17
像这样的简单 HTML 代码 $html = "<p>Hello, it's me!</p>";
有效。
也 PHP 这样的代码 $html = "My name is ".$row['name']."!";
有效。
只是 if 语句似乎不起作用。
我做错了什么?
$html = "text... " . (!empty($row['...'])) ? $row['...'] : " " . " more text...";
删除 if 并创建一个 'inline' 或上面的三元表达式。
If 语句不能串联,除非使用 shorthand 表达式(即:三元)
对于您的情况,这应该有效:
$html = " " . (!empty($row['name'])) ? "Your name is " . $row['name'] : "" . " ";
对于更长的连接,你可以这样做(和可读性):
$html = "";
$html .= (expression) ? True : False;
$html .= "";
为了连接,我们在 PHP 中使用 .=
。
我建议在 PHP 中长期使用 print_f()
方法 HTML,例如。
print_f("your name is %s", (!empty($row['name'])) ? $row['name'] : 'Default');
你不能在中间用 if
连接字符串(因为 if
语句没有 return 任何东西)。
您可以使用三元运算符解决此问题:
$html = "
".(!empty($row['name']) ? "My name is".$row['name'] : '')."
";
如果你有很长的 if-else 语句,你应该关闭字符串并在里面连接它:
$html = "";
$html .= "Some string\n";
if (....) {
$html .= "Something new\n";
} elseif (...) {
$html .= "more...\n";
} else {
$html .= "...\n";
}
$html .= "Some text\";