Thursday, 28 April 2016

echo - PHP Error when echoing string



I want to echo my String but i have encountered some Problems.
The error I get is:




Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING




Code:




for($i = 1; $i <= 31; $i++){   
$Text="";
echo $Text;}

Answer



You have PHP tags mixed in with your attempt at string concatenation. Take you if statement out and put it onto its own statement. Then add that variable to your string:



$Text=""



becomes:



$selected = (@$_POST['geb_tag'] == $i) ? ' selected' : '';
$Text="";


A couple of things:





  1. Your string is messy. There are better way to add PHP variables to strings to make them more readable.


  2. Don't use @. You should check to see if that variable is set instead.




.



$selected = (isset($_POST['geb_tag']) && $_POST['geb_tag'] == $i) ? ' selected' : '';
$Text= "";

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...