View Single Post
Old 06-11-2009, 09:06 PM   #9 (permalink)
Village Idiot
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Hehe. Nonsense! But perhaps we can evolve this thread into something a little more meaningful

I used to wrap a lot of my items in functions. For $_GET I used to have g(), $_POST was p(), and $_GET/$_POST was gp(). These are all fine and dandy but they don't describe too well what is happening inside that function. Whether it's setting or getting, what it's setting or getting.

I was working with the open-source eCommerce system, Magento, a couple of weeks ago now, and I noticed, much to my disgust, that they were using function names with two underscores (__) to print labels. What were they thinking?

For instance:

php Code:
function __($szText)
{
    return $szText;
}

echo __('I love TalkPHP');

As it is, however, I much prefer the use of print and echo. They describe well what is happening, and in themselves they are not functions, but rather language constructs. Wrapping them in functions, insofar as I see, isn't preferable, particularly in terms of readability.

In addendum, I even dislike using printf , especially in amongst echoes and prints, because, from a superficial perspective, they are highlighted differently in Zend Studio, and the printf requires the items to be passed as arguments, unlike print and echo. It creates a mess!

I now always follow the rule that everything must describe, as best as they can, what its purpose is.
Printf was passed on from C to PHP. Since PHP is loosely typed and allows automatic siring joining, printf is effectively useless. However in C, things are harder. Printf is the best way to output things in C. Likewise, sprintf is the easist way to assemble a string together. In C, a string is an array of chars (one charictar), so C works with the following
Code:
//bad assignment
char x;
x = "Hello World";

//bad assignment
char x[3];
x="Hello World";

//Valid assignment
char x[4];
x="Hello";
x="12345";

//Attachment
char x[4];
char y[4];
char xy[9];
x="Hello"
y="World"
xy = x+y; //Bad
xy = x&y; //Bad
xy = x.y; //Bad
sprintf(xy,"%s%s",x,y) //Good
As you can see, sprintf is the easiest way in standard C to join strings. There is a library that makes strings work something closer like what they do in PHP, but that is not as platform independent.
__________________

Village Idiot is offline  
Reply With Quote