473,769 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1344
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 internationalis ation ...

--
PETER FOX Not the same since the statuette business went bust
pe******@eminen t.demon.co.uk.n ot.this.bit.no. html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.dem on.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 internationalis ation ...
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)

iD8DBQFFnkUwyeC Lzp/JKjARArNIAJ9LIP x7YE+rvVlbVjgt3 5trTunZqACaApNB
lRTJEr/mE6yaMAJonc1Yf9 c=
=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
19152
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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
3
14870
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++. My questions are 1. How good a language is C++ for network programming? 2. what are good onine resources to learn C++ network programming? 3. How similar it is to Java socket programming? Thanks in advance.
133
8594
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
16
8509
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
2589
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
1512
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 to this.
23
2387
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 follow for creating a program? I am writing a map game program. I created several objects: board object that is an array of integers each number 0-5 is a kind of terrain, a terrain object that is an array of terrain types and each number of...
22
2723
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 programming. I tend to learn what is useful and gets the job done. I am always curious if there is some techique I don't know. I read books and study as well as write programs. My goal is to some day be able to get a job programming. I have a...
36
1921
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 code bases are mostly written in C++. I read somewhere about the best way to learn C (or a programming language in general). I agree with the points. I quote them below: "The best way to do it is to read some stuff written by masters of...
20
3093
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 like 65% of the time of the second routine. Yet both do the same thing. However, the second one seems better in terms of the way the code is written since it helps encapsulate the transformation in the inner loop better making it easier to read,...
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.