better programming

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

A quick and most likely simply question about which is generally better  
programming with PHP.  Is it more proper to break out of PHP code to write  
HTML, or is it ok rto do it within print() statements?  Such as...

<img src="<?=$imgsrc?>">
...more code...

print("<img src=\"".$imgsrc."\">");
...more code...


Re: better programming

Michael wrote:
Quoted text here. Click to load it

It highly depends, and there is no easy answer. I usually break out of php
for large blocks, and stay inside for small bits.

However, the reason I post;
1. <?=$imgsrc?> is a Bad Thing
    It can work, no question about that, but always try to use a proper
<?php echo ''; ?>.

2. print("<img src=\"".$imgsrc."\">");
    Better would be either print("<img src=\"\">"); or print('<img

Rik Wasmus

Re: better programming

Following on from Michael's message. . .
Quoted text here. Click to load it

Of course there is no 'best', 'best practice' or 'proper' [Hey will  
posters /please/ stop asking for 'best practice'!]

Personally I _write in PHP_ and so print() everything.  This gives you  
the flexibility to stop hard-coding any time you please.

For example :
$address = "57 High St., Huddersfield";  // could be define()
print("You can visit us at $address Monday to Friday 9 - 5");

$address can be derived from an include/configuration file.  This means  
there is one more thing to go wrong at initial setup but it also means  
that it can cope with moving premises AND setting up a second location  
in a single place.

It also means that if you think 'objects' you can emit objectively (as  
it were) rather than HTML-ing every instance.

This gets more serious when you want to mix and mingle with CSS.  An  
'Address' object might be used for printing postal address to a screen,  
a postcode to a map URL or a company address for an invoice (using DX  
for solicitors etc.).

And if you're ever thinking of internationalisation ...

PETER FOX Not the same since the statuette business went bust
2 Tees Close, Witham, Essex.
Gravity beer in Essex  <

Re: better programming

Peter Fox wrote:
Quoted text here. Click to load it

Good point:

$format['visitus'] = array(
    'en' => 'You can visit us at %s Monday to Friday 9 - 5',
    'nl' => 'U kunt ons van maandag tot vrijdag tussen 9 - 5 op %s

$lang = 'en'; //or somthing else


Makes it a lot simpler to change languages then the other option.
Rik Wasmus

Re: better programming

Why don't you try to separate your code from the html part completely,
e.g. with Smarty ( ) ???

Michael wrote:
Quoted text here. Click to load it

Re: better programming

Hash: SHA1

Martin Mandl - m2m tech support schrieb:
Quoted text here. Click to load it

Good call, thought nobody would suggest this IMHO best solution for the
proposed question, where I don't mean that Smarty is the best solution,
but the idea of separating business logic from representation.


Quoted text here. Click to load it

Version: GnuPG v1.4.2.1 (MingW32)


Re: better programming

Michael wrote:

Quoted text here. Click to load it

I find code easier to read if it is almost entirely PHP or almost entirely
HTML.  Therefore, large blocks of static HTML with little or no dynamic
content will use html:

// php code goes here....
  <h1>Welcome to the Wizard!</h1>
  <p>Welcome, <?=$user_id?>, to our wizard, we hope
     that you will find it easy to use, and all
     of that nonsense.
// ...more php code

However, if I am stringing together text that is more variables than static,
it tends to look better and be easier to read if it is a list of PHP

$row=SQL_OneRow("Select * from customers...")
echo "Hello ".$row['user_id'];
echo ", I see you are from ".$row['city'];
echo ", and that you have ".$row['eyecolor']." eyes";

Quoted text here. Click to load it

Kenneth Downs
Secure Data Software, Inc.

Site Timeline