PHP - echo html and variables, what am I doing wrong?!

Associate
Joined
3 Nov 2005
Posts
611
I am being baffled by something which should be so simple and seemingly looks correct as I've done something similar a long way up the code which works fine. Be grateful if you can help as I want to get this sorted.

echo "<input type='hidden' name='blog_id[]' value='" . $postid . "'>";
echo "<input type='text' name='blog_title[]' value='" . $editlist2['Title'] . "'>"
echo "<textarea id='editpost' name='blog_post[]'>" . $editlist2['Post'] . "</textarea>";
echo "<h1>This post originally contains " . $length . " characters. You may add up to " . $char_allowed . "more.</h1>";
echo "<h2>This post was first created by " . $editlist2['User'] . " - " . $editlist2['DateTime'] . ".</h2>";

The bottom three lines of the five are all causing issues. When I comment the third line out, the fourth one errors and so on.

The error is - 'Parse error: parse error, expecting `','' or `';'' in...'
 
In the longer term, it's bad practice to code how you have (I think). I'm not a professional coder but personally doing it like that (with whole lines in PHP for no reason) can raise difficulties later and just isn't tidy.

Try something like this instead:

Code:
<input type="hidden" name="blog_id[]" value="<?echo $postid?>">
<input type="text" name="blog_title[]" value="<?echo $editlist2["Title"]?>">
<textarea id="editpost" name="blog_post[]"><?echo $editlist2["Post"]?></textarea>
<h1>This post originally contains <?echo $length?> characters. You may add up to <?echo $char_allowed?> more.</h1>";
<h2>This post was first created by <?echo $editlist2["User"]?> - <?echo $editlist2["DateTime"]?></h2>
 
In the longer term, it's bad practice to code how you have (I think). I'm not a professional coder but personally doing it like that (with whole lines in PHP for no reason) can raise difficulties later and just isn't tidy.

I have been debating which way to do that whether to just open/close a php tag for including variables in HTML but I read around on the subject on a few websites who suggested it was more a matter of readability than any real speed increase.

I do see your point though....I might change it round if others agree with you :)
 
Do ANY servers actually require the long tag anymore (ie won't function with just the short?).

It depends on the configuration of short_opentag in the php.ini but most hosts leave it turned on as it's the default config.

You can also get into the whole whitespace/tab/closing tag arguments but it's a matter of style.
 
Do ANY servers actually require the long tag anymore (ie won't function with just the short?).

dunno. i'm a complete amateur myself. :p

and tbh i don't think there's anything particularly wrong with using the echo statements like the OP is. i just prefer to wrap html in single quotes though. it's slightly quicker as php won't be looking for variables to parse.
 
Short tags have been the default for years and are convenient so why not use them?

Also, instead of that unwieldy echo, you can just use:

Code:
<?= $foo ?>

...to echo $foo
 
There are obviously arguments for avoiding short tags, and certainly when you're writing, say, a big, public project that needs to be portable I wouldn't dream of using them. But when you're targeting your own server on which you know they're enabled? It's a needless and artificial restriction—save yourself the keystrokes :)
 
Or this:

Code:
<input type="hidden" name="blog_id[]" value="<?= $postid ?>">
<input type="text" name="blog_title[]" value="<?= $editlist2["Title"] ?>">
<textarea id="editpost" name="blog_post[]"><?= $editlist2["Post"] ?></textarea>
<h1>This post originally contains <?= $length ?> characters. You may add up to <?= $char_allowed?> more.</h1>";
<h2>This post was first created by <?= $editlist2["User"] ?> - <?= $editlist2["DateTime"] ?></h2>

EDIT: Just saw someone else suggested it
 
personally i prefer the OP style but he 'complexised' it. Using double quotes means the variable don't have to be 'unquoted' so this will work:
Code:
echo "<input type='hidden' name='blog_id[]' value='$postid' >";
echo "<input type='text' name='blog_title[]' value='$editlist2['Title']' >"
echo "<textarea id='editpost' name='blog_post[]'>"$editlist2['Post'] </textarea>";
echo "<h1>This post originally contains $length characters. You may add up to $char_allowed more.</h1>";
echo "<h2>This post was first created by $editlist2['User']  - $editlist2['DateTime'].</h2>";

It may be worth reading up on the use of quotes in PHP - it confused me at first, but i's really quite simple.
 
In the longer term, it's bad practice to code how you have (I think). I'm not a professional coder but personally doing it like that (with whole lines in PHP for no reason) can raise difficulties later and just isn't tidy.

Try something like this instead:

Code:
<input type="hidden" name="blog_id[]" value="<?echo $postid?>">
<input type="text" name="blog_title[]" value="<?echo $editlist2["Title"]?>">
<textarea id="editpost" name="blog_post[]"><?echo $editlist2["Post"]?></textarea>
<h1>This post originally contains <?echo $length?> characters. You may add up to <?echo $char_allowed?> more.</h1>";
<h2>This post was first created by <?echo $editlist2["User"]?> - <?echo $editlist2["DateTime"]?></h2>

I have read about the issues of echoing huge amounts of text, but as far as I know couple of rows like the OP has shouldn't be a problem really.
 
Back
Top Bottom