472,809 Members | 2,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,809 software developers and data experts.

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 1306
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
3
by: Jay | last post by:
Hi, I implemeneted an FTP client and server long time back using Java. I found sockets porgramming in java quite useful and easy to handle. I now wanted to implement a similar program using C++....
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
33
by: Protoman | last post by:
Which is better for general-purpose programming, C or C++? My friend says C++, but I'm not sure. Please enlighten me. Thanks!!!!!
10
by: Protoman | last post by:
Which is better for general-purpose programming, C or C++? My friend says C++, but I'm not sure. Please enlighten me. Thanks!!!!! PS I believe the folks over at comp.lang.c++ could add something...
23
by: JoeC | last post by:
I am a self taught programmer and I have figured out most syntax but desigining my programs is a challenge. I realize that there are many ways to design a program but what are some good rules to...
22
by: JoeC | last post by:
I am working on another game project and it is comming along. It is an improvment over a previous version I wrote. I am trying to write better programs and often wonder how to get better at...
36
by: istillshine | last post by:
I personally like C, and do not like any OO languages. The reference books for OO languages are too heavy for me. They just made things complicated. Someone laughed at my opinion, saying Google...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.