Okay, this may seem a bit elitist, depending on your point of view. But though I realize that I'm probably a bit more educated than a number of people out there who never finished high school, I fail to see why some people can't remember basic elementary school stuff.
I'm reading the "Visual Quickstart Guide" for PHP scripting, to get myself familiar with the basics of PHP scripts before I dive back into a more advanced book that I purchased a while back, but had difficulty with some of the more advanced stuff and figured I'd start at the very beginning. In the section concerning mathematical operations, specifically the issue of multiple operators (like "10 - 4 / 2")... rather than bother to tell people to remember the basic rules of precedence, he just tells them to use parentheses all the time.
Doesn't anyone learn "Please Excuse My Dear Aunt Sally" these days?
</rant>
-------------------- “Those people who think they know everything are a great annoyance to those of us who do.” — Isaac Asimov Star Trek Minutiae | Memory Alpha
Registered: Nov 2000
| IP: Logged
Charles Capps
We appreciate your concern. It is noted and stupid.
Member # 9
posted
Different languages have different ideas about operator precedence. Knowing how often PHP breaks backwards compatability, it wouldn't surprise me if it changed from version to version.
Using parens is the best way to forcefully ensure that your idea of precedence is enforced.
Registered: Mar 1999
| IP: Logged
Cartman
just made by the Presbyterian Church
Member # 256
posted
Plus, they improve legibility of your code.
-------------------- ".mirrorS arE morE fuN thaN televisioN" - TEH PNIK FLAMIGNO
Registered: Nov 1999
| IP: Logged
quote:Originally posted by Charles Capps: Different languages have different ideas about operator precedence.
Well, that's brilliant! Probably another example of IT people ensuring their job security by making certain that no one can figure out what they're doing.
quote:Knowing how often PHP breaks backwards compatability, it wouldn't surprise me if it changed from version to version.
Interesting... I took a six-week class at the university learning basic C programming, and from what I've been able to discern, PHP has a lot of similarities, at least on the surface in terms of coding language.
quote:Using parens is the best way to forcefully ensure that your idea of precedence is enforced.
Oh, I know. But this guy didn't even TRY to explain the concept of precedence, but rather sidestepped it altogether.
I mean really... if you're trying to learn a major computer programming language, isn't it logical to figure that people have basic math skills and know the order of operations?
-------------------- “Those people who think they know everything are a great annoyance to those of us who do.” — Isaac Asimov Star Trek Minutiae | Memory Alpha
Registered: Nov 2000
| IP: Logged
Charles Capps
We appreciate your concern. It is noted and stupid.
Member # 9
posted
PHP is to web apps as Visual Basic is to Windows apps.
Any moderately complex PHP application written, say, for version 4.06, will almost surely NOT function for the latest version, 4.3.3. Why? Because they seem to break the most basic functionality in every single version.
In my experience, many people that call themselves PHP programmers have no clue whatsoever. Ask an average PHP programmer about operator precedence, and they'll say something to the effect of "WTF? Use parens!"
Thus your answer.
The same PHP programmers also tend to be completely unaware of security, HTML filtering, and other critical things that you HAVE to do for web applications.
If you must learn PHP, learn it AFTER you learn at least another few languages... Hell, even Java would be better to learn first.
(Of course, being the resident Perl guy hasn't influenced my opinion of PHP at all. Nope. Not a bit.)
Registered: Mar 1999
| IP: Logged
posted
Once I wrote a program that calculated interest on bank loans in Basic. I colored the text red and green, and I think I made it go "beep" when it was done.
Maybe it was more of a "bloop."
Yeah, that's what it was.
Registered: Oct 1999
| IP: Logged
Cartman
just made by the Presbyterian Church
Member # 256
posted
Move over, Mitnick.
Registered: Nov 1999
| IP: Logged
posted
Interesting perspective there, Charles... Actually, from what I already know about programming, I could tell a few things about PHP, so your distaste doesn't surprise me too much. (Stuff like not needing to declare variables before you use them; I know that's something you need to do!)
So then... would you suggest some alternative to PHP for integrating with MySQL databases? 'Cause that's why I've been learning it...
-------------------- “Those people who think they know everything are a great annoyance to those of us who do.” — Isaac Asimov Star Trek Minutiae | Memory Alpha
Registered: Nov 2000
| IP: Logged
Charles Capps
We appreciate your concern. It is noted and stupid.
Member # 9
posted
Why, Perl, of course. %)
Perl database access is easy, but tends to be obscure unless you know what you're looking for. As with everything Perl, there are two and a half billion different ways of doing things...
Here, have some comparison code that gets a list from the db, then updates... PHP first...
code:
$dbh = mysql_connect("localhost", "user", "pass"); if(!$dbh) { echo "Could not connect to database: " . mysql_error(); exit; } // end if
// Make a query... $result = mysql_query("select * from table where col_1 = 3", $dbh); if(!$result) { echo "Could not perform query: " . mysql_error(); exit; } // end if
// ... then pull the info out of the DB, line by line, into an array $results = Array(); $i = 0; while($resline = mysql_fetch_array($result)) { $results[$i] = $resline; $i++; } // end while
// We now have our results array. Loop through it and do stuff. // (using the $i index from above as our array length rather than // sizeof($results), just because I'm weird) for($j = 0; $j < $i; $j++) { $returned_val = do_something_smart_with_row($results[$j]); $sql = "update table set col_2 = '$returned_val' " . where col_1 = '" . $results[$j]["col_1"] . "'"; $thisres = mysql_query($sql); if(!$thisres) { echo "Could not perform query: " . mysql_error(); exit; } // end if } // end for
mysql_close($dbh);
And now, in Perl:
code:
use DBI; our $dbh = DBI->connect("DBD:mysql:mydb", "user", "pass") or die "Could not connect to database: $DBI::errstr";
# $dbh is the database handle
# Query from the DB and get all the results in a single hash my $results = $dbh->selectall_hashref("select * from table where col_1 = 3") or die "Could not perform query: $DBI::errstr";
# Loop through the hash and do stuff foreach my $key (keys %{$results}) { my $returned_val = &do_something_smart_with_row($results->{$key}); my $sql = "update table set col_2 = '?' where col_1 = '?'" # the ?s get replaced with the 2nd+ arguments in the # do() method of the database handle... $dbh->do($sql, undef, $returned_val, $key) or die "Could not perform query: $DBI::errstr"; } // end foreach
$dbh->disconnect();
The main difference between the code, other than the verbosity, is that all the database functions in the PHP script are database-specific, and all the database functions in Perl are generic. While PHP does have a database abstraction layer, it's not built into the language. (Then again, neither is Perl's DBI... but considering that DBI is all there is to connect to a DB in the first place, that's no big loss.)
Thus, moving the Perl script to another database system later is just a matter of making sure the SQL works, while every single command would need to be translated in the PHP script. This annoyed a coworker so much that, when he transitioned to PHP from Perl, he created a PHP object that duplicated the parts of DBI that he used.
Perl also has a number of other advantages in the convenience departments... for instance, having to escape quotes in prints/echos is a major annoyance in PHP... but in Perl, you can just define another quote character for that one string. I.e.:
$php = "There \" are \" too\"\" many" . '\'quotes\' in' . "\" this\" string"; $perl = qq~There " are " too"" many 'quotes' in" this " string~;
PHP does have its advantages.. often, hosts will compile everything and the kitchen sink into PHP, meaning often it can do graphics, PDF generation, and other nifty things "right out of the box"... Perl can do all of the same things, but hosts often don't compile the modules in by default... and compiling modules would require shell access.
PHP also often has a major speed advantage. Once compiled and running, PHP and Perl scripts are pretty much the same speed... but the startup/compile phase is a killer. PHP is often compiled right into the web server, meaning it practically stays resident in memory. PHP also has the ability to keep cached compiled copies of scripts in memory, which can be a major speedup.
Doing the same things in Perl requires the mod_perl Apache module, which isn't something many shared hosting providers offer. (Writing code for mod_perl is also rather complex. You can't just plug your script in and expect it to work without modification.. sometimes as little as two or three lines, thankfully)
Anyway. I've ranted enough. Personally, I prefer Perl because of the consistancy and the flexability of use and syntax. I don't care for PHP because it gets horrible inconsistant, forces the authors to be very verbose (huge function names) and has a number of forced syntax quirks.
Registered: Mar 1999
| IP: Logged
Can you predict a thread's future by decrypting this code?
This sucks: I forgot my decoder ring.
Stop me before my wise-ass refrences drift into codes of conduct like Bushido....
-------------------- Justice inclines her scales so that wisdom comes at the price of suffering. -Aeschylus, Agamemnon
Registered: Aug 2002
| IP: Logged
quote: Hell, even Java would be better to learn first.
Well... except you then end up using PHP as an object-oriented language, and PHP objects don't behave like they do in a real OO language, and I've heard it's not particularly efficient either in PHP.
Another thing about PHP is it's easy integration with HTML. While this might look very nice in the beginning, it turns into a readability nightmare for anything larger than 10 lines.
Cartman
just made by the Presbyterian Church
Member # 256
posted
No, but really, Perl doesn't float my boat. It's lazy, man... every single freakin' keystroke has to be correct or you're slapped with an undefined subroutine &main::find_next called at frob.pl line xx unless you resort to symbol::approx::sub which can make your life easier BUT then you get into all sorts of trouble with the autoload function which is mainly used to write even lazier accessors for object data because Perl stores all information about non-lexical variables and filehandles and subroutine names in a massive hash which you can inspect yourself BUT doing so requires code which is so close to spaghetti you could plate it up and serve it to an Italian so the easy way out is to use devel::symdump which (thankfully) provides a friendly and clean interface to the symbol table and various other neat tools like if you are scratching your head trying to resolve an inheritance tree the isa_tree method will help and if you want to find exactly what a module exports into your namespace the diff method is a constant friend as are the eighty billion date and time modules you never need but are there for your amusement nonetheless.
Registered: Nov 1999
| IP: Logged