Forum Topic: PHP: A rant in a lesson in a rant

(343 views • 25 replies)

This topic is 1 page long.

<< < > >>
Mad as Hell

authorblues

Reply To Post Reply & Quote

Posted at: 12/28/08 05:41 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

im sure im not the only one to get annoyed by this, but why is it that people who lack skill and understanding of PHP insist on a single, annoying, ignorant mistake. yes, it works, but it is wrong. what, you might ask, am i talking about? i will get to that, but first...

i want to make it very clear that making the following mistake is a distinct sign of being incapable as a programmer. why? because you are bad at an easy language. have a hard time with pesky type checking? dont like complicated syntax? no problem. PHP (because of its perl roots) is a sincerely simple language. and you cant even use THAT!?

the mistake, my friends, can be found in the following code:

$result = nl2br("$source");

so, why do people insist on putting their string parameter in double quotes. what does this do? well, first the double quotes tell the parser to start interpretting special tokens. then we tell it the name of the string. then it replaces the token with the string. wait, wait, wait! we did what now? you mean, we HAD a string already, and we went through all that extra work?

how about, instead, we stop being retarded and start doing what we mean:

$result = nl2br($source); // same result, less work

now, i know what youre thinking. "why should this bother you? its harmless." no, what it does is it belies the way the programmers mind works (or doesnt). someone, along the line, taught the lesson that if $result is "Yes", something like "Result: $result" will produce "Result: Yes". so, the inept programmer thinks "well, i dont want the 'Result:' part, so i must need to do "$result"".

this shows the programmer never actually LEARNED anything. they just took bits and pieces of code and figured that if the result is what we expect, then everything must be correct. but you couldnt be more wrong. just because the end is correct doesnt mean that the path to get there was acceptable.

a little protip, offered from me, to the lot of you, free of charge, is this: know what your code does. dont just regurgitate useless code written by useless people, and eventually picked up by useless script kiddies. KNOW (truly know) what your code does.

</rant>

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Neptunus

Reply To Post Reply & Quote

Posted at: 12/28/08 05:49 PM

Neptunus DARK LEVEL 07

Sign-Up: 12/19/08

Posts: 97

It's a good tip. Much of the blame lies with people who teach PHP (through writing tutorials or otherwise) that do exactly this. Such people should of course not be teaching, but it's so vomit out tutorials on the internet, and stumbling upon the wrong ones can be bad for the neophyte programmer.


None

DFox

Reply To Post Reply & Quote

Posted at: 12/28/08 05:54 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,490

Well said babe, well said.

Now I get to say what I HATE that PHP developers do. And I've seen people ranging from noobs to professional PHP developers do this, and I've never understood why.

echo "authorblues is my daddy.";

Do as many tests as you want on this, DOUBLE QUOTES ARE SLOWER THAN SINGLE QUOTES. We all know why, it's not difficult to understand. Double quotes tell the parser to look for and replace variables, single quotes don't. This applies to anything including function calls ex: mysql_escape_string($avar . "why is this in double quotes, do we like slower scripts?").

Anyway, why do people insist on not doing this instead:

echo 'authorblues is my daddy.';

Was that so hard?

And I'm not going to get in to concatenation, because I could see the 1 advantage to using double quotes, ease, but concatenation using double quotes instead of single is STILL slower than just using single.


None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 12/29/08 06:00 AM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,284

Following up on DFox...does anyone else HATE it when people do...

echo "$variable";

?


None

25272D

Reply To Post Reply & Quote

Posted at: 12/29/08 06:17 AM

25272D NEUTRAL LEVEL 02

Sign-Up: 10/22/08

Posts: 353

At 12/29/08 06:00 AM, WoogieNoogie wrote: Following up on DFox...does anyone else HATE it when people do...

echo "$variable";

?

I'm guilty of the whole:

echo "Hello, welcome to my php snippet!";

I only make small applications for myself and others, nothing for heavy use, so I've never noticed the difference in speed, but it annoys me when people don't just do:

echo $variable;

It's common sense.


None

BoneIdol

Reply To Post Reply & Quote

Posted at: 12/29/08 10:38 AM

BoneIdol NEUTRAL LEVEL 05

Sign-Up: 08/14/06

Posts: 819

The following have pissed me off about PHP and websites I've worked on over the past 2 years (this is not an exhaustive list):

- Using parenthesis on echo and print, they are language constructs not functions
- echo "$variable"; (as mentioned)
- Using double quotes
- Using single quotes and .s
- . being the concatenate operator
- PHP scripts with syntax errors that mysteriously work then give wrong results when you fix them
- Having register globals on
- Commenting out lines of code with no explaination
- Too many comments
- Not enough comments
- Stupid comments
- Using mysql_* functions
- Using mysql_* functions and not using mysql_escape_string
- Using mysql_escape_string on integers (we have typecasting for that)
- mysql_real_escape_string
- PHP warnings being turned off through development then suddenly turned on on the server.
- safe_mode
- Using parametised queries
- Using stored procedures
- Not using switch/case statements
- Using switch/case statements with fall throughs
- Not using switch/case statements with fall throughs
- Not using JOINS in SQL (Eew, n+1 queries)
- Using JOINS in SQL (Eew, so slow on big databases)
- Caching
- Using PDO
- Using PDO stupidly
- Using PDO instead of nice libraries a framework offers
- Using PHP's OOP
- Retarded use of PHP's OOP
- Not using PHP's OOP
- Magic functions
- Serialize and deserialize functions
- OOP having no messaging/event system
- Try/catch statements
- Using really high level OOP concepts I don't understand very well
- Not using constructors
- Using regular expressions
- Not using regular expressions
- Using stupid regular expressions
- Rewriting built in PHP functions
- Using built in functions that annoy me (ala pathinfo)
- Weaving PHP blocks in between HTML
- Using templates
- Using template languages
- Too many include files
- Not enough include files
- Not using a framework
- Using a framework
- MVC
- Using MySQL
- Using other databases than MySQL
- Using SQLite (this deserves a special mention)
- Using a stupid, house-written framework
- Using a framework with horrible documentation (CakePHP)
- Using classic ASP
- Mixing classic ASP with PHP
- Not redirecting upon dealing with post data
- Sending emails via sendmail
- Sending emails via qmail
- Using MIME formatted emails
- Using HTML emails
- Using Multipart emails
- PHP 4
- Code written not to work in PHP 4
- PHP 6
- Unicode
- Not using basename on file uploads
- Using cron jobs
- Not being able to use cron jobs
- Using system calls
- Building pages from one index.php file and a load of $_GET vars, without using mod_rewrite
- Using a single include file to deal with all your $_POST data
- XML being used instead of a database
- XML being used
- Scripts off of free script sites
- Regular expressions off of free script sites (deservers another special mention)
- $_FILES superglobal being ordered ['name/tmp_name/etc.']['index'] instead of having the index first
- etc. etc...

So I have come to the conclusion that I am contrary, irritable and bitter.

*Wraps flu-ridden-self in asbestos blanket*

Sufficiently advanced incompetence is indistinguishable from malice.


None

DanielZ

Reply To Post Reply & Quote

Posted at: 12/29/08 10:45 AM

DanielZ DARK LEVEL 08

Sign-Up: 07/30/08

Posts: 566

I'm guessing your XML hatred has come from adam2510, right?

BBS Signature

None

Jon-86

Reply To Post Reply & Quote

Posted at: 12/29/08 10:55 AM

Jon-86 NEUTRAL LEVEL 13

Sign-Up: 01/30/07

Posts: 3,930

At 12/29/08 10:38 AM, BoneIdol wrote: - Using parenthesis on echo and print, they are language constructs not functions

Out of that list

echo('this is the one a do often, so their...')

But also:
- Using switch/case statements with fall throughs
- Not using switch/case statements with fall throughs
- Using PHP's OOP
- Not using PHP's OOP

And a few other contradictions.....

Youat the wind up or something

PHP: A rant in a lesson in a rant

PHP Main :: C++ Main :: Java Main :: irc.freenode.net

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 12/29/08 11:08 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,418

At 12/29/08 10:55 AM, Jon-86 wrote: And a few other contradictions.....

They're occurrences, not definite cases.

PHP is such a code monkey language. Neko is not meant to be read by humans yet it's much better than PHP.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Jon-86

Reply To Post Reply & Quote

Posted at: 12/29/08 11:19 AM

Jon-86 NEUTRAL LEVEL 13

Sign-Up: 01/30/07

Posts: 3,930

At 12/29/08 11:08 AM, GustTheASGuy wrote: They're occurrences, not definite cases.

Gotcha

PHP Main :: C++ Main :: Java Main :: irc.freenode.net

BBS Signature

Resigned

MasterDragonClock

Reply To Post Reply & Quote

Posted at: 12/29/08 11:40 AM

MasterDragonClock DARK LEVEL 04

Sign-Up: 11/08/06

Posts: 140

Wait... I've only seen one person doing that, and I laughed and called them a faggot. Then I got banned.

BBS Signature

None

SweetSkater

Reply To Post Reply & Quote

Posted at: 12/29/08 12:57 PM

SweetSkater LIGHT LEVEL 11

Sign-Up: 03/18/06

Posts: 1,910

$lol = lulz;
echo "$lol";

amidoinitrite?

Owner of Spam The Web.

BBS Signature

None

Cinjection

Reply To Post Reply & Quote

Posted at: 12/29/08 01:49 PM

Cinjection LIGHT LEVEL 18

Sign-Up: 04/06/04

Posts: 2,490

Wow. I've never seen anyone do that, yet (thankfully). That's just a text book sign that the programmer copy and pasted code from the internet and fiddled with it until it worked. It's a shame that there are so many programmers out there that do things like that.

The best part about going on Newgrounds is that no one know that you're naked!
[My Coder Profile]

BBS Signature

None

DFox

Reply To Post Reply & Quote

Posted at: 12/29/08 01:52 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,490

At 12/29/08 10:38 AM, BoneIdol wrote: - Using single quotes and .s
- . being the concatenate operator

I don't get these two. Please explain.


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 12/29/08 02:17 PM

Afro-Ninja EVIL LEVEL 38

Sign-Up: 03/02/02

Posts: 13,467

Ha, at first glance I thought you were going to say that the use of nl2br was unacceptable.

BBS Signature

None

BoneIdol

Reply To Post Reply & Quote

Posted at: 12/29/08 04:22 PM

BoneIdol NEUTRAL LEVEL 05

Sign-Up: 08/14/06

Posts: 819

At 12/29/08 02:17 PM, Afro-Ninja wrote: Ha, at first glance I thought you were going to say that the use of nl2br was unacceptable.

REAL programmers use a regular expression to generate <p> tags for semantically correct markup. ;)

Also, just to clarify; all those were over the course of 2 years and as I mentioned I am a contrary bastard. Generally anything other programmers do in PHP tends to piss me off; it just depends on my mood, how good the coffee was that morning and whether or not I'm hung over.

Sufficiently advanced incompetence is indistinguishable from malice.


None

elbekko

Reply To Post Reply & Quote

Posted at: 12/29/08 09:00 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,587

I already ranted about this last year or so.
Yes, it makes me cringe and go look for a sharp battle axe each time I see it.

There are many things that annoy me in PHP, be it by others or just the language itself :P Can't be arsed to list it tho.

"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

None

littleMonsterGames

Reply To Post Reply & Quote

Posted at: 1/2/09 05:44 PM

littleMonsterGames DARK LEVEL 05

Sign-Up: 12/24/08

Posts: 338

At 12/28/08 05:54 PM, DFox wrote: some stuff about how double quotes are slower than singles

Okay I'm guilty of that. It's not ignorance - I'm just lazy. But I have been trying to steer away from "" recently.

By the way, for those of you who don't know (I'd assume you do, but you never know :/):

Double quotes are slower because php has to check every character to see if it's a $ (start of a string). With single quotes, it doesn't have to do that.

The LittleMonsterGames website: http://www.littlemonstergames.com - super fun, I promise :)

BBS Signature

None

littleMonsterGames

Reply To Post Reply & Quote

Posted at: 1/2/09 05:49 PM

littleMonsterGames DARK LEVEL 05

Sign-Up: 12/24/08

Posts: 338

I'd just like to add that in MY opinion, although single quotes are always more efficient, it can be better to use double quotes if you have LOTS of strings and stuff. If you have just a couple, single quotes should be fine, but if your text has like tons of strings, you might be better off just using double quotes.

Oh, and also as a continuation from my last post, double quotes are also less efficient because php has a harder time finding the end of a string. With single quotes, it just looks for the dot. But with double quotes, one of several things could mark the end of a string.

The LittleMonsterGames website: http://www.littlemonstergames.com - super fun, I promise :)

BBS Signature

None

elbekko

Reply To Post Reply & Quote

Posted at: 1/2/09 05:54 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,587

Oh, and also as a continuation from my last post, double quotes are also less efficient because php has a harder time finding the end of a string. With single quotes, it just looks for the dot. But with double quotes, one of several things could mark the end of a string.

Wait, what? Finding the end of the string is done by finding the next unescaped quote...

Also, how is having alot of strings a good reason to use double quotes? If anything, it's a good reason NOT to, since it'll all be slower.

The only time you should be using double quotes is if you need to insert control characters.

"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

None

littleMonsterGames

Reply To Post Reply & Quote

Posted at: 1/2/09 06:58 PM

littleMonsterGames DARK LEVEL 05

Sign-Up: 12/24/08

Posts: 338

At 1/2/09 05:54 PM, elbekko wrote:
Oh, and also as a continuation from my last post, double quotes are also less efficient because php has a harder time finding the end of a string. With single quotes, it just looks for the dot. But with double quotes, one of several things could mark the end of a string.
Wait, what? Finding the end of the string is done by finding the next unescaped quote...

Well, not really...

'text'.$string.'text'
...the string ended before the period. But i see your point, which is that it looks for the next non escaped quote before it continues reading the text normally again.


Also, how is having alot of strings a good reason to use double quotes? If anything, it's a good reason NOT to, since it'll all be slower.

What I meant was that generally it's good practice to use single quotes (which we've all agreed upon). When I said having a lot of strings means you should use double quotes, I just meant that if you have a situation where there is just a little bit of normal text but TONS of strings, it might be better to just use double quotes, and take the tiny efficiency hit.

For example:

"hi $string$string a $string$array[$number] and $string";

has very little normal text, and lots of strings - I'm just saying that if it's a piece of code that you're going to be working with and changing every so often, it's a bit easier to change the above than the below.

'hi '.$string.$string.' a '.$string.$array[$number].' and '.$string;

The only time you should be using double quotes is if you need to insert control characters.

In the end though, I'm just going to admit that you're right. I see my point's main weakness, which is laziness. No more unnecessary double quotes for me!

The LittleMonsterGames website: http://www.littlemonstergames.com - super fun, I promise :)

BBS Signature

None

elbekko

Reply To Post Reply & Quote

Posted at: 1/2/09 10:05 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,587

In the example you gave, it's actually a lot clearer with the concatenation. I don't know if you've ever had to trace obscure bugs in large projects, but if you have, you must know that finding the wrong variable inside a string isn't exactly simple. Escaping the string gives you some extra hints at where the dynamic content is, where you variables are, etc. Especially with a syntax highlighter.

I must admit, I still have the habit of using double quotes in queries (ofcourse still getting out of them to insert variables), I try justifying it by that it looks messy to constantly be escaping single quotes :P I know I'm wrong on that, but still, it stays in my fingers :P

"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

None

Thomas

Reply To Post Reply & Quote

Posted at: 1/3/09 01:30 AM

Thomas LIGHT LEVEL 13

Sign-Up: 02/14/05

Posts: 2,830

Since reading this post, I have stopped using double quotes (unless needed).

Although, I still use them if I make a form that displays a link:

$link = "<a href='http://link.com'>my link</a>";

I suppose it it really mattered, I could do this:

$link1 = 'http://link.com';
$link2 = '<a href='.$link1.'>my link</a>';

Anyways, I'm glad this thread was posted. I'm still learning as I go...


None

DFox

Reply To Post Reply & Quote

Posted at: 1/3/09 01:49 AM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,490

At 1/3/09 01:30 AM, Thomas wrote: Since reading this post, I have stopped using double quotes (unless needed).

Although, I still use them if I make a form that displays a link:

$link = "my link";

I suppose it it really mattered, I could do this:

$link1 = 'http://link.com';
$link2 = 'my link';

Am I missing something or couldn't you just do:

$link = '<a href="http://link.com">my link</a>';

?


None

DFox

Reply To Post Reply & Quote

Posted at: 1/3/09 01:54 AM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,490

Also, someone above stated that the time difference between single quotes and double quotes is pretty much negligible. That greatly depends on what you're definition of negligible is. If it's speed gained in one run of the script, then chances are yes, you can call the speed advantage of single quotes negligible. But how negligible is it if you have single quotes in a loop that's running 10,000 times in one script instead of double quotes in that loop. Or even more feasible, how about the extra work/processing the server has to do if a page that uses mostly double quotes instead of single is requested 100,000 times per minute? Those are some things to think about.


None

adam2510

Reply To Post Reply & Quote

Posted at: 1/3/09 03:01 AM

adam2510 EVIL LEVEL 13

Sign-Up: 10/02/05

Posts: 3,725

At 12/29/08 10:45 AM, DanielZ wrote: I'm guessing your XML hatred has come from adam2510, right?

fucksake

i'm not even going to try anymore

Web Hosting: Free! Premium!

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 05:30 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!