3

I am trying to include a value from a database table within the value element of an input field.
This is what I have, but it is not working:

?><input type="text" size="10" value="<?= date("Y-m-d", 
strtotime($rowupd['upcoming_event_featured_date'])) ?>" name="upcoming_event_featured_date" 
id="keys"/><?php

I have done this before, but I usually print it out like this:

print '<input type="text" size="10" value="'.date("Y-m-d", 
strtotime($rowupd['upcoming_event_featured_date'])).'" name="upcoming_event_featured_date" 
id="keys"/>';

What is the appropriate way of doing this without using print ''?

1
  • I imagine there are various ways of doing this, but now the question I have is; which is best? Commented Oct 23, 2008 at 19:21

4 Answers 4

3

It's a good idea to always use full PHP tags, because that will keep your app from breaking if you move to a different server or your config is changed not to allow short tags.

?>
<input type="text" size="10" value="<?php
echo(date("Y-m-d", strtotime($rowupd['upcoming_event_featured_date'])));
?>"name="upcoming_event_featured_date" id="keys"/><?php

Also, note that you are missing the ; from the end of your PHP code.

You may find it better to keep the whole thing in PHP too, and just echo() out the HTML, as that will keep you from having to switch back and forth from PHP to HTML parsing.

Sign up to request clarification or add additional context in comments.

2 Comments

one-line echoes don't need a semicolon.
They may not be completely necessary, but It's still a good idea to have them.
2

As some other replies have mentioned, make sure short_open_tag is enabled in your php.ini. This lets you use the <?= syntax. A lot of people recommend not using short tags, since not all servers allow them, but if you're sure you won't be moving this to another server, I think it's fine.

Besides that, I don't know of any technical reason to choose one way over the other. Code readability should be your main focus. For example, you might want to set your value to a variable before outputting it:

$featured_date = date("Y-m-d",strtotime($rowupd['featured_date']));

?><input type="text" value="<?=$featured_date?>" name="featured_date" /><?php

In fact, I'd try to do as little processing as possible while you're in the middle of a block of HTML. Things will be a lot cleaner if you define all your variables at the beginning of the script, then output all of the HTML, inserting the variables as needed. You're almost getting into templating at that point, but without needing the overhead of a template engine.

Comments

1

You can use the short_open_tag ini directive to turn on the <?= shortcut for printing.

If that is not available, you must use either print or echo to accomplish this.

You can try:

ini_set('short_open_tag', true);

Comments

0

You can do

[...] ?><input 
    type="text" 
    size="10" 
    value="<?php echo date("Y-m-d", strtotime($rowupd['upcoming_event_featured_date'])) ?>" 
    name="upcoming_event_featured_date" 
    id="keys"/>
<?php [...]

if you don't have short_open_tag enabled in your PHP configuration.

Comments

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.