468,768 Members | 1,534 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,768 developers. It's quick & easy.

better programming

Hi,

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...

SCRENARIO A)
<?php
....code...
?>
<img src="<?=$imgsrc?>">
<?php
....more code...
?>

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

Thanks,
Michael
Jan 4 '07 #1
6 1184
Rik
Michael wrote:
Hi,

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...

SCRENARIO A)
<?php
...code...
>>
<img src="<?=$imgsrc?>">
<?php
...more code...
>>

SCENARIO B)
<?php
...code...
print("<img src=\"".$imgsrc."\">");
...more code...
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=\"{$imgsrc}\">"); or print('<img
src="'.$imgsrc.'">');

Grtz,
--
Rik Wasmus
Jan 4 '07 #2
Following on from Michael's message. . .
>Hi,

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...
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
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Jan 5 '07 #3
Rik
Peter Fox wrote:
For example :
$address = "57 High St., Huddersfield"; // could be define()
print("You can visit us at $address Monday to Friday 9 - 5");
And if you're ever thinking of internationalisation ...
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
bezoeken.');

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

printf($format['visitus'][$lang],$adress);

Makes it a lot simpler to change languages then the other option.
--
Rik Wasmus
Jan 5 '07 #4
Why don't you try to separate your code from the html part completely,
e.g. with Smarty (http://smarty.php.net) ???

Michael wrote:
Hi,

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...

SCRENARIO A)
<?php
...code...
?>
<img src="<?=$imgsrc?>">
<?php
...more code...
?>

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

Thanks,
Michael
Jan 5 '07 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Martin Mandl - m2m tech support schrieb:
Why don't you try to separate your code from the html part completely,
e.g. with Smarty (http://smarty.php.net) ???
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.

Thanks
Stefan
>
...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.1 (MingW32)

iD8DBQFFnkUwyeCLzp/JKjARArNIAJ9LIPx7YE+rvVlbVjgt35trTunZqACaApNB
lRTJEr/mE6yaMAJonc1Yf9c=
=PicZ
-----END PGP SIGNATURE-----
Jan 5 '07 #6
Michael wrote:
Hi,

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...
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.
<?php
// ...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
commands:

$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";

>
SCRENARIO A)
<?php
...code...
?>
<img src="<?=$imgsrc?>">
<?php
...more code...
?>

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

Thanks,
Michael
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jan 10 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

220 posts views Thread by Brandon J. Van Every | last post: by
16 posts views Thread by Robert Zurer | last post: by
33 posts views Thread by Protoman | last post: by
10 posts views Thread by Protoman | last post: by
23 posts views Thread by JoeC | last post: by
22 posts views Thread by JoeC | last post: by
36 posts views Thread by istillshine | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.