
Journal cyranoVR's Journal: It's official: I don't like PHP 46
I should preface this quasi-rant by saying that the online documenation for php is the best on the net, if only for the user comments. Of course, after the experience I had this weekend "trying it out," I understand completely why extensive online documenation is of vital necessity.
Backstory: I had an idea for a small web application, so I decided to try coding it in PHP. I was not a pleasant experience, and I really with I had speant it learning more about a useful java framework or something. However, somehow I had gotten the impression that PHP is a relatively "simple" language. I thought I should look into it - maybe it would be handy for future projects.
Where should I start? I spent most of the weekend puzzling over the languages' bizzare variable and scoping conventions. You're supposed to be able to use include() and require() to break up your code into managable files - but I saw right away how this could become a nightmare. Variables with scope across 2 or 3 different files...trying to keep track of them all...my head's starting to hurt again just thinking about it.
See, in ASP 2.0 and even in JSP, you can read settings from a database or file, and then store them in an Application-scoped variable. For example, ASP's Application object. This feature allows you to have true application settings available everywhere - FAST - rather than having open a database connection *every time* you need them.
Yes, I realize that "bad programmers can make any language suck" or whatever. However, I am starting to see how languages have their strengths and weaknesses.
One thing I like about java is that it forces everyone to write good code. No global variables or any of that crap. You always have an idea of what object you're working with, and if you're not sure (i.e. it's in an array of Object) you're not allowed to do anything with them.
On the other hand, Java isn't suited to just "banging something out" over the weekend.
I know most people will scoff at this, but if I had to choose between Microsoft VBScript for ASP 2.0 and PHP, I'd choose ASP/VBScript. I mean, I know people give VBScript shit for not being a "real" language, but damn. At least you can distinguish your array variables from your regular scalars - and even gives you some useful collection objects like scripting.Dictionary.
And the database programming is a lot more consistant and (dare I say it) object based than PHP's procedural function calls (mysql_db_connect, mysql_query, etc.) Nothing beats a good ol'
Server.CreateObject("ADODB.Recordset")
rs.Open strConnString, strSQL
But I digresss. The thing is, Microsoft VB/VBScript gets a lot of flack for not being a "real" programming language exactly because they focus on business related tasks and stay away from "real" programming (e.g. mundane computer sciencey stuff like pointers and registers and whatever).
The funny thing about PHP is that it tries to be a "simple" language (no variable types to speak of, procedural coding encouraged) but unlike VB retains all of the complexity. Why was I spending so much time trying to figure out how to initialize and/or populate an array? WTF? Where is my java.util.HashMap? Where is my Dictionary.Scripting? Hell, I'd take vba.Collection at this point!
And why the hell can't I make a user-defined function? Why do I keep getting a "funciton GetPageLanguage() undefined!" error when I clearly defined the funciton? WTF? Why doesn't require() work as advertised?
Why can't I declare my variables? Without an IDE, I have to spend most of my time tracking down typos. VB/VBA has Option Explicit. What is the PHP equivalent? To the person who is enevitably going to reply telling me that there is an equivalent keyword (perhaps var?) answer me this: why is the feature such a closely guarded secret? I want to declare my variables, dammit!!!
I mean, even perl has my.
So anyway...Java keeps it clean and Microsoft keeps it braindead. I guess PHP lets you get to show off your 733t skills. Or something.
I also understand that the new version is more object-oriented and moving closer to java in a lot aways...but I just can't get past that variables thing. Let's face it: PHP borrows heavily from Perl, which itself was a derived language (C, sed, awk, etc). And now they're moving towards Java (???) So now you are going to have applications that mix object-oriented and procedural paradigmns with global scoping and all sorts of redundant functions.
It's going to be like Perl on steriods! Except with none of the charm.
How did terrific applications like phpMyAdmin even get built. I'm thinking a lot of all-nighters spent chasing down typos.
No thanks.
Re: (Score:1)
Re:some things (Score:2)
First of all, I couldn't get the globals keyword to work satisfactorily, nor the $_GLOBALS variable.
My point about ASP (and JSP) having an Application object is that the feature is standard with the framework and therefore is availalbe in every langage. I'm thinking about using PHP for "real" applications here. The notion that every PHP project has to re-invent this makes my head spin. Maybe it's in PHP5?
What's co
Typo alert (Score:2)
"My point about ASP (and JSP) having an Application object is that the feature is standard with the framework and therefore is available - and, hopefully, used - in every project."
Re:some things (corrected font formatting) (Score:2)
As I recall, you're the fellow that likes things when they're obscure, correct? Anyways...
First of all, I couldn't get the globals keyword to work satisfactorily, nor the $_GLOBALS variable.
My point about ASP (and JSP) having an Application object is that the feature is standard with the framework and therefore is availalbe in every langage. I'
WTF Is your problem? (Score:2)
PHP has destroyed my brain. Never again.
Bedtime.
Re: (Score:1)
/me rolls eyes (Score:2)
As for this
function query($mysqlhost,$mysqluser,$mysqlpass,$mysqldb,$ m ysqlquery)
{
$conn = mysql_connect( $mysqlhost, $mysqluser, $mysqlpass);
mysql_select_db($mysqldb);
$query=mysql_query($mysqlquery);
while($row=mysql_fetch_assoc($que
Re: (Score:1)
Re:/me rolls eyes (Score:2)
Also, I discovered print_r($array) but it's out put didn't help me that much.
I'm running php 4.3.8, and while ya, I don't think your storing the data quite right, the foreach works fine on mine
I rest my case
Obviously I'm doing something wrong, it's not a bug etc. etc. I've read Eric Raymonds "How to Ask
Re: (Score:1)
Re:/me rolls eyes (Score:2)
They wouldn't ship a buggy release of PHP with their code...
would they?
Btw - see my thread with blazin for a resolution of the "invalid argument to foreach" - it turns out it was the use of [] brackets inside the foreach() that was causing the problem.
Re: (Score:1)
Re:/me rolls eyes (Score:2)
Anyway...
Re:/me rolls eyes (Score:2)
I'd gues you were really wanting to get:
Using $languages[] adds whatever you are setting to the end of the array with a new numerical index.
I think this all comes down to what
Re:/me rolls eyes (Score:2)
You just made me realize why I was having so much trouble...it was exactly because I was trying to make an "array of arrays" like we do in java i.e. each element of the first array is merely a pointer to the second.
When I saw that you could, in PHP,
Re:/me rolls eyes (Score:2)
broken.php (Score:2)
produces the following output:
/var/www/html/broken.php on line 11
Warning: Invalid argument supplied for foreach() in
the problem, it would seem, is using the brackets inside the foreach(). This version:
foreach($LA
Re:broken.php (Score:2)
Re:broken.php (Score:2)
The resultset is used for constructing a menu that is used on every page ("select language:"). However, while I want to store this "lookup table" in a database, it doesn't make sense to do a new mysql query on every single page.
What I wanted to so was create a $_SESSION['MENU'] variable and then
This is "caching" - the user only has to query the datab
Re:broken.php (Score:2)
Yup, PHP doesn't really have an equivalent. It stays closer to the HTTP model of request->reply->forget everything. Using sessions can get around that but you probably don't want to have a copy of the menu for every user. Then you're ju
Re:broken.php (Score:2)
Re:broken.php (Score:2)
??? Why would I do that?
Yes, the first file had tags around the function.
It was probably the missing @, whatever that means (directive?).
Re:broken.php (Score:2)
Re:broken.php (Score:2)
Argh...YES.
Look, if you're really interested, I'll take a look tonight and see if I can recreate the behavior.
Re:broken.php (Score:2)
Re:broken.php (Score:2)
I'm not "angry" just irritated...as in "yes, I understand how includes work" - relative paths, virtual paths, filesystem paths etc etc.
I'm not really interested in furthering my PHP knowledge for now - however, in the interest of closure, I'll post the require() code.
Re:broken.php (Score:2)
I'd be interested in seeing the require code.
Sample code (Score:2)
For some reason, index.php has no output. Nothing!
Meanwhile, below is links to the code for the two specific pages I we are discussing. Feel free to discover what I'm doing wrong and post your analysis here...
index.php [cs.com]
global.p [cs.com]
Re:Sample code (Score:2)
global $blah;
then it would also work. So that's the weirdness of PHP scoping and globals right there.
For your code, I've got the following problems:
When I load it up unedited, I get
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in global.php on line 96
I believe the parse error
Re:Sample code (Score:2)
Re:Sample code (Score:2)
I say again: wtf?
Re:Sample code (Score:2)
Your sample index.php shouldn't output anything since $blah in that context isn't defined.
I get that. Incidentally, I did try global $MENU_HTML hoping that the function could see it...still no go.
Let me point out also that java lexical scoping works very differently: instance methods can see "up" into member variables but classes can't see "down" into the local (er...instance
Re:Sample code (Score:2)
Dammit, forgot about the (Score:2)
foreach($LANG_LIST[] ; as $foo)
The brackets (which I meant to embolden) were the source of my woes.
I'm using php 4.2.2, btw
Re:Dammit, forgot about the (Score:2)
The line should most likely be
foreach($LANG_LIST as $foo)
Re:Dammit, forgot about the (Score:2)
STUPID! STUPID! STUPID!
Use what you know, or learn (Score:2)
Without trying to be harsh, this looks a lot like the complaints of "Open Office doesn't act exactly like MS Word, so it's worthless" that I've heard for years. And I don't even like php that much!
Unless that's very different to what I think it is then you want to look at array().
Re:Use what you know, or learn (Score:2)
That's about right...I'm sure if I gave PHP a chance I could figure it out...however, given the time investment I already have in Java, and FortKnox's pending site project, it's not worth it for me to invest more time in learning php.
My biggest compaints with PHP were that I was already familiar with python/pe
Re:Use what you know, or learn (Score:2)
Yeh, I avoided PHP for a long time because I could always do what I wanted in perl or python ... only eventually using when I had to maintain someone else's php code. And so I'd def. recommend stay with Java, as that's what you know ... but that doesn't make PHP bad, just
Bad langs, bad programers (Score:1)
Bad Languages can make any programmer suck.
Re:Bad langs, bad programers (Score:2)
However, I think it's an objective statement that java forces its programmers to be explicitly clear about what they're doing.
i.e. ven if you have stupid variable names, you can't get around the strict typing and overly-long variable names.
Compare with php which mixes everything up and hides a lot of information from you (unless you want to make lots of is_bool() is_string() is_array() calls).
Of course, VBScript has only one variable
Re:Bad langs, bad programers (Score:1)
BTW, PHP is a freakishly awful language. I do most of my coding in VB and Perl (which has switches to change it from "freakishly awful" to "exceptional" with a flip of a -t -w -s), although I'm starting to try to wrap my head around Java.
Your comment here explains exquisitely the reasons for my burning loathing of PHP. =)
Re:Bad langs, bad programers (Score:2)
Thanks sincerely for that tip!
It's amazing how productive I'm already becoming with perl...I already wrote a prototype of a script that checks to make our online banking server is up and emails us automatically if its not, and later I'm going to write one that will rename 1000+ httpd log files in a way that can be sorted more easily...and then one that backups all the files I modified for the day t
Re:Bad langs, bad programers (Score:1)
Re:Bad langs, bad programers (Score:2)
Wow...you came out and wrote exactly what I was thinking!
While I was coding Perl, I realized that it "feels" a lot like VB6/VBA...but I wasn't sure if stating that outright would draw agreement of condemnation...
Re:Bad langs, bad programers (Score:1)