-1

I am trying to execute a php code inside a variable, that prevents a text print if a variable is empty. The code is as follows:

$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");
}

I am facing problem at:

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

This code basically prevents the "Item 2 : " from printing if the @$_POST['Item2'] from the form is empty.

The code I am using for this is here: https://stackoverflow.com/a/24857671/5008955

I am getting the error "Parse error: syntax error, unexpected '?'" with this line.

Thanks for your help.

2
  • why you are using again php tags it's already it in php only Commented May 13, 2018 at 14:42
  • you can try this one $f_data= "<?php if (isset($Item2) && !empty($Item2)) { ?>" 'Item 2 : '.$Item2.'}'; Commented May 13, 2018 at 14:43

1 Answer 1

0

You appear to be entering PHP tags and then leaving them twice.

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

You should have the following:

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

You are also checking if $Item1 and $Item2 are set, which regardless if there is any data received from the form, those variables will be set as you have defined them.

So you could make it:

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

Or one step further:

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

You may also wish to write it like this:

// 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 ";
}
Sign up to request clarification or add additional context in comments.

8 Comments

I still get the error "Parse error: syntax error, unexpected '?'" when I use '<?php if (!empty($Item1)) { ?>' Item 1 : '.$Item1.' ' <?php } ?>'
Still getting the same error. Can you show me the whole code so that I could know 'm not doing anything wrong?
What data are you entering for $Item1 and $Item2?
The $Item1 and $Item2 data is retrieved from form submission via $Item1 = @$_POST['Item1'];
Sorry I meant, what data are you entering in to the form when you are testing it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.