如果php变量为空,则隐藏行

如果php变量为空,则隐藏行,php,Php,我试图在一个变量中执行一个php代码,如果变量为空,它将阻止文本打印。代码如下: $file = 'file.txt'; if (file_exists($file)) { $counter += file_get_contents($file); } file_put_contents($file, $counter); $Item1 = @$_POST['Item1']; $Item2 = @$_POST['Item2']; $filename = "mydata.txt";

我试图在一个变量中执行一个php代码,如果变量为空,它将阻止文本打印。代码如下:

$file = 'file.txt';

if (file_exists($file)) {
    $counter += file_get_contents($file);
}
file_put_contents($file, $counter);

$Item1 = @$_POST['Item1'];
$Item2 = @$_POST['Item2'];

$filename = "mydata.txt";
$txt = "\n";

$f_data= '
'<?php if (isset($Item1) && !empty($Item1)) { ?>' Item 1 : '.$Item1.' '} ?>'
'<?php if (isset($Item2) && !empty($Item2)) { ?>' Item 2 : '.$Item2.' '} ?>'
';

$file = fopen($filename, "a");
fwrite($file,$f_data);
fclose($file);
}
else
{
die("error");
}
$file='file.txt';
如果(文件_存在($file)){
$counter+=文件获取内容($file);
}
文件内容($file,$counter);
$Item1=@$_POST['Item1'];
$Item2=@$_POST['Item2'];
$filename=“mydata.txt”;
$txt=“\n”;
$f_数据=
''项目1:'.$Item1'.'}?>'
''项目2:'.$Item2'.'}?>'
';
$file=fopen($filename,“a”);
fwrite($file,$f_数据);
fclose($文件);
}
其他的
{
死亡(“错误”);
}
我面临的问题是:

'<?php if (isset($Item1) && !empty($Item1)) { ?>' Item 1 : '.$Item1.' '}'
'<?php if (isset($Item2) && !empty($Item2)) { ?>' Item 2 : '.$Item2.' '}'
“项目1:”.$Item1.“}”
“”项目2:“.$Item2.”
如果表单中的@$_POST['Item2']为空,此代码基本上会阻止打印“Item 2:”

我使用的代码如下:

这一行出现错误“解析错误:语法错误,意外“?”


感谢您的帮助。

您似乎在输入PHP标记,然后两次离开它们

'<?php if (isset($Item1) && !empty($Item1)) { ?>' Item 1 : '.$Item1.' '} ?>'
'<?php if (isset($Item2) && !empty($Item2)) { ?>' Item 2 : '.$Item2.' '} ?>'
或者更进一步:

'<?php echo (empty($Item1)) ?: " Item 1 : $Item1 "?>'
'<?php echo (empty($Item2)) ?: " Item 2 : $Item2 "?>'

为什么您再次使用php标记?它已经在php中了。您可以尝试使用这一个$f_data=“”'Item 2:'。$Item 2'.}';当我使用“”项1:“.$Item1.”时,仍然会出现错误“Parse error:syntax error,意外的“?”,仍然会出现相同的错误。你能给我看一下完整的代码吗?这样我就可以知道“我没有做错任何事吗?你为
$Item1
$Item2
输入了什么数据?通过$Item1=@$\u POST['Item1',从表单提交中检索$Item1和$Item2数据;对不起,我的意思是,在测试表单时,您在表单中输入哪些数据?
'<?php if (!empty($Item1)) { ?>' Item 1 : '.$Item1.' ' <?php } ?>'
'<?php if (!empty($Item2)) { ?>' Item 2 : '.$Item2.' ' <?php } ?>'
'<?php echo (empty($Item1)) ?: " Item 1 : $Item1 "?>'
'<?php echo (empty($Item2)) ?: " Item 2 : $Item2 "?>'
// Set the $f_data variable so that if neither $Item1 or $Item2 
// have any data, your `fwrite($file,$f_data);` won't fail.

$f_data = '';

if (!empty($Item1)) {
    $f_data .= "Item 1 : $Item1 ";
}
if (!empty($Item2)) {
    $f_data .= "Item 2 : $Item2 ";
}