TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Tips & Tricks (http://www.talkphp.com/tips-tricks/)
-   -   Tired of Print - (Just an idea lol) (http://www.talkphp.com/tips-tricks/4517-tired-print-just-idea-lol.html)

codefreek 06-10-2009 02:07 AM

Tired of Print - (Just an idea lol)
 
PS: i know, I have to much time on my hands haha

Print, Got Owned ;)

PHP Code:

<?php
// End the Use of print : )
function p($s "") {
print 
$s;
}
p(lol);
?>



Quote:

in theory you will use less text p? then print.
/just a thought

-Cf

Wildhoney 06-10-2009 02:23 AM

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.

codefreek 06-10-2009 02:40 AM

Thanks for the comment. ;) // always so insightful :)
and hum Magento a (__) thats yeah hum.. maybe a typo XD how
epic would that be if it was a typo hehe..

tony 06-10-2009 03:50 AM

jeje, solia hacerlo todo el tiempo con java para hacer el System.out.println() corto a println() pero con php no es tanto escribir print o echo, ademas de que me gusta el sintaxis de concatenar con comas en la declarativa de echo.

Nota Personal: Buscar otra alternative de eCommerce y no usar magento. jaja



Translation:

Quote:

I used to do it all the time with java to do the System.out.println () to short println () with php but not so much to write print or echo, plus I like the syntax for concatenating with commas in the declaration of echo.

Personal Note: Find another alternative to not using eCommerce and magento.
English Please.
Translation Added - By: CodeFreak

Sakakuchi 06-11-2009 08:38 AM

it's quite funny, I recently had a system on my desk which also used __ for printing. But that stuff went through an translation system later on.

tony 06-11-2009 03:11 PM

I like p() better than __() at least it refers to printing somewhat. :P
is echo/print too long to use?

P.S. my bad :P The weird thing is that originally I wrote it in English then translate it to Spanish. I must of thought to be in another forum.
Thanks for the translation CodeFreak, I didn't know you know Spanish.

codefreek 06-11-2009 06:39 PM

Tony, i am a person with many talents ;)

Randy 06-11-2009 08:24 PM

I am going to agree with wildhoney, its nice to make it shorter to type however there is still the issue of remember what letter stands for what.

Village Idiot 06-11-2009 09:06 PM

Quote:

Originally Posted by Wildhoney (Post 25141)
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.

rguy84 06-15-2009 07:49 AM

WordPress has a __() function. I forget the details but if you said __('Archives') the word archives would be translated into the visitors language. I don't know why the whole site wasn't wrappped by default but whatever

Mike 06-24-2009 11:56 PM

well... the __() function is more a gettext function, for i18n, wordpress has this function to return translated strings.
Wordpress also wrap echo __('some text') in _e('some text')

I guess creating "shorcuts" is a good idea when you have to repeat a lot of preoccesses over and over again, but in other hand, creating your "own language" is not a good idead, at least your developing a framework :P


All times are GMT. The time now is 09:56 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0