Welcome Guest, Not a member yet? Register   Sign In
View code that hurts your brain
#1

[eluser]xwero[/eluser]
I just saw this blog post and glancing over it i saw this snippet
Code:
<html>

<head>

<title><?php echo $title;?></title>

</head>

<body>

<h1>&lt;?php echo $header;?&gt;</h1>

<ul>

&lt;?php foreach($users as $user):?&gt;

<li>

<p>&lt;?php echo 'Full Name: '.$user['firstname'].' '.$user['lastname'].' Email: '.$user['email'];?&gt;</p>

</li>

&lt;?php endforeach;?&gt;

</ul>

<p>&lt;?php echo 'Total number of users :'.$numusers;?&gt;</p>

&lt;/body&gt;

&lt;/html&gt;
What make people echo strings in a view file?

My version
Code:
&lt;html&gt;

&lt;head&gt;

&lt;title&gt;&lt;?php echo $title;?&gt;&lt;/title&gt;

&lt;/head&gt;

&lt;body&gt;

<h1>&lt;?php echo $header;?&gt;</h1>

<ul>

&lt;?php foreach($users as $user):?&gt;

<li>

<p>Full Name: &lt;?php echo $user['firstname'].' '.$user['lastname']; ?&gt; Email: &lt;?php echo $user['email'];?&gt;</p>

</li>

&lt;?php endforeach;?&gt;

</ul>

<p>Total number of users :&lt;?php echo $numusers;?&gt;</p>

&lt;/body&gt;

&lt;/html&gt;
I know many little php escapes can slow down the page rendering but this doesn't mean you have to put a paragraph in a single echo? What if you want to add html tags then it become even uglier that it is now.
#2

[eluser]Randy Casburn[/eluser]
This could be a DWYK thing right? Or maybe a just a personal preference as you've described. Sometimes when I get in a hurry "just to get it done" there is no telling what things may look like. There could be other possibilities. Perhaps there is a roadmap that requires future multilingual features.

When considering multilingual sites it is even different than either of these potentially. For instance:

Code:
&lt;?php echo 'Full Name: '.$user['firstname'].' '.$user['lastname'].' Email: '.$user['email'];?&gt;

may become...

Code:
&lt;?php echo $lang.fullname.': '.$user['firstname'].' '.$user['lastname'].$lang.email.': '.$user['email'];?&gt;

If that's the case, the global search and replaces would 'seem' to be much easier to accomplish.

But don't know. Just thinking along with you.

Randy
#3

[eluser]xwero[/eluser]
DWYK???

Even if you have to do something quick, which isn't the case here as it's a blog post snippet, isn't it more logical to put stings outside the php code? To take it to the extreme lets say you use a template engine, you are not going to write
Code:
<p>{'Full name: '.$user['firstname'].' '.$user['lastname'].' Email: '.$user['email']}</p>
you write
Code:
<p>Full name: {$user['firstname']} {$user['lastname']} Email: {$user['email']}</p>
Why would that be different using php as a template engine?

And if a site goes multilanguage isn't it better to create language dependent views instead of making each individual text language dependent? What happens if an individual text in one language must be split in more pieces to have the same meaning?

And the question remains what if you want to use tags in the string
Code:
<p>&lt;?php echo '<span class="fn">Full Name: '.$user['firstname'].' '.$user['lastname'].'</span><br><span class="email">Email: '.$user['email'].'</span>';?&gt;</p>
Then you have tags in and outside php. I don't think that is a very good idea because it's inconsistent.
#4

[eluser]Hannes Nevalainen[/eluser]
A little php-performance tip is not to concat strings before output (use comma instead of a dot) it's little faster.
Code:
echo 'username: ' , $user->username;

Happy Coding.
#5

[eluser]Randy Casburn[/eluser]
[quote author="xwero" date="1219437733"]DWYK??? [/quote]

Sorry...meant UWYK from http://ellislab.com/forums/viewthread/88487/ :red:

[quote author="xwero" date="1219437733"]And if a site goes multilanguage isn't it better to create language dependent views instead of making each individual text language dependent? What happens if an individual text in one language must be split in more pieces to have the same meaning?[/quote]

Really? A view for every possible language you want to support? You can't mean that. Is that what you really mean?

[quote author="xwero" date="1219437733"]And the question remains what if you want to use tags in the string
Code:
<p>&lt;?php echo '<span class="fn">Full Name: '.$user['firstname'].' '.$user['lastname'].'</span><br><span class="email">Email: '.$user['email'].'</span>';?&gt;</p>
Then you have tags in and outside php. I don't think that is a very good idea because it's inconsistent.[/quote]

I can't imagine putting any HTML tags into my PHP unless it is necessary part of the page generation. But that's a convention thing for me...and a personal preference. Maybe something I've learned. Cuts down on the spaghetti code in the code block above. Wouldn't do it.

So I suppose I'm trying to keep my mind open to all kinds of coding styles. Maybe the blog entry was solely demonstrative. But...

[quote author="xwero" date="1219437733"]... isn’t it more logical to put stings outside the php code?[/quote]
Yea...I'm with you on this.

@Hannes - Sounds like fun - benchmark this page up. I'd like to see how many nanoseconds we're different. I can see this leading to the inevitable "double quotes are slow" performance debate. Fun.
#6

[eluser]m4rw3r[/eluser]
From what I remember of a page discussing that with echo and commas, echo was run for every data sent to it.

So this calls echo once:
Code:
echo $firstname.$lastname;

This calls it two times (but in C, so it is still fast):
Code:
echo $firstname, $lastname;

If I recall, this makes commas faster in some instances and slower in others.
#7

[eluser]Hannes Nevalainen[/eluser]
Call
Code:
//This calls echo ONE time, but with many arguments...
echo $firstname, ' ', $lastname;
The speed boost is just marginal but since it's just as easy to write a comma intead of a dot so why not?

Actually double quotes are slower to use, and when you don't need your string to be parsed it's better to use single quotes. I allways use singlequotes when i'm working with pure strings, but when it's makes the code easier to read I use double quotes. This won't probably be the bottleneck of your app but it's just so easy to use those single quotes (or commas) instead. =)

Code:
$a = 'world';


$b = "hello $a"; //Easy.

$b = 'hello ' . $a; //Not so easy.

I don't wan't to start a war or something so maybe we can go back to the subject of this thread.
#8

[eluser]xwero[/eluser]
Randy creating language dependent views that contains a certain amount of text are easier to maintain and modify than placing language dependent strings everywhere in the views. I'm not talking about views that only contain single words like most forms but views that have phrases in them who contain variables.
Quote:10 emmertjes water halen
Translates into
Quote:Getting 10 buckets of water
10 is the variable here. This is what i meant saying a continuous string in one language isn't always a continuous string in another language. Sure you can use sprinf but for language editors it's easier to understand if you show them
Code:
&lt;?php echo $number ?&gt; emmertjes water halen
Than
Code:
%s emmertjes water halen
You may not understand the phrase but you know the variable will be a number.

From experience i learned people who want multilanguage sites are quicker to translate words/phrases in context than out of context even if they have to write the same things more than once because of it.

Working with language files can also get out of hand on sites that require a lot of words and phrases. Sometimes there can be a slight difference of meaning in the word between languages in a certain context making it necessary to create a new word entry for that language but then you have to check the context if you only have language dependent words. If you use a language dependent view there is less hassle in these situations.

Bottom line translations are a very tricky business to program.
#9

[eluser]Randy Casburn[/eluser]
[quote author="xwero" date="1219462931"]Randy creating language dependent views that contains a certain amount of text are easier to maintain and modify than placing language dependent strings everywhere in the views. [/quote]

So I think you're saying you do a view per language.

[quote author="xwero" date="1219462931"]From experience i learned people who want multilanguage sites are quicker to translate words/phrases in context than out of context even if they have to write the same things more than once because of it. [/quote]

Yes, this makes sense, although it isn't the convention...

Here are several examples of other locationalization implementations (both PHP and JavaScript) that store strings in arrays or JS objects and either use them directly as in my silly example above, or use a more fomalized OOP approach to use a function call of some sort in a template replacement scheme. It's actually quite a common method. I didn't include the Zend Framework's i18n implementation because it is so very, very formal, but it does the "strings in an object/array" replaced in a template thing. I couldn't imagine creating a new, duplicated view for every language my site would have to support. I know that can't be what you mean.

http://book.cakephp.org/view/161/localiz...nalization
http://trac.seagullproject.org/wiki/Howt...nalisation
http://extjs.com/learn/Tutorial:Localizing_Ext
http://www.etomite.com/forums/index.php?...showcat=15
http://plugins.jquery.com/project/gettext
http://recurser.com/articles/2008/02/21/...on-plugin/

I'm still with you on the whole string thing. But I put my translations in strings, in arrays, in objects, and filter, and gettext() my way with the replacements. And hope the translators can see the context in the original template. Knowing that what you say above makes sense...of course.
=========
Quote://This calls echo ONE time, but with many arguments...
echo $firstname, ' ', $lastname;
Um, OK, but...

Just for fun I looked up the PHP source. ECHO is a C macro (ZEND_ECHO_SPEC_CV_HANDLER) declared in zend_vm_execute.h in the Zend directory of the PHP source.

The echo C MACRO is iterated for each comma separated value provided to it and sends that to zend_print_variable(). So this function is executed n times with 'n' being the number of strings passed in the comma separated list...So I guess it depends on what the definition of what ONE is.
========
Sorry for the divergence xwero here...

Randy




Theme © iAndrew 2016 - Forum software by © MyBB