Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - what is the proper way to do inline HTML variable output?

Tags:

html

php

I just moved from an Ubuntu PHP workstation dev environment back to Windows and am using xampp. I have a bit of code, like so:

<input type="text" name="txtEmail" value="<?=$emailaddress;?>"/>

that I swear worked to display the variable in the textbox when I was developing before. But when I loaded the page on Windows/xampp it just put that text between the quotes in the textbox. Instead, I ended up changing it to something like:

<input type="text" name="txtFirstName" value="<?php echo($firstname);?>" />

The latter makes sense, but I guess I thought there was a shorthand or something, and I must be going crazy because I'm sure the first way was working on a difference environment.

So what's the best way to do this?

like image 844
Peter Tirrell Avatar asked Apr 23 '10 01:04

Peter Tirrell


People also ask

How does PHP work with HTML?

If you want to represent web-pages, you need to use HTML markup. PHP is merely a programming language which is (in this context) used to dynamically generate HTML markup. So, if you request a PHP file, and expect a web-page in return, the PHP script has to generate the HTML markup and send it in the response.


2 Answers

When doing inline commands the semi-colon isn't required. I personally prefer the short tags, typing <?php echo gets old after a while.

like image 56
Adam Gotterer Avatar answered Oct 12 '22 02:10

Adam Gotterer


Save yourself a headache and don't use short tags. They feel dirty, they don't work on every setup, and I think they may be getting phased out of php altogether (That might be wrong though).

Even if you know how to fix them, you'll still be irritated every time you have to change your new server to make them work.

Edit: Yup, they're getting depracated. http://cubicspot.blogspot.com/2009/06/maximum-failure-php-6-deprecates-short.html

2017 Edit: To add an important distinction - the <?= "short echo" format is always enabled (see Dav's comment below). The <? "short open" format is a separate matter. For more info see Why are "echo" short tags permanently enabled as of PHP 5.4?

like image 34
Syntax Error Avatar answered Oct 12 '22 02:10

Syntax Error