Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
PHP

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.

This discussion was created by cyranoVR (518628) for Friends and Friends of Friends only, but now has been archived. No new comments can be posted.

It's official: I don't like PHP

Comments Filter:
  • Comment removed based on user account deletion
    • 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'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
    • I should have written:

      "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."
    • [Note to FortKnox, if you're reading this: the site should allow users the ability to go back and edit their comments (but perhaps with a "comment edited" flag)]

      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'
      • Reposting a comment twice to correct typos?

        PHP has destroyed my brain. Never again.

        Bedtime.
      • Comment removed based on user account deletion
        • Re: your affinity for obscurity: You probably forgot about my experience trying to install MPlayer from a year ago, which never quite worked like it should. MPlayer's documentation at the time was widely known to suck, and IIRC you didn't see a problem with that.

          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
          • Comment removed based on user account deletion
            • Suffice to say: everyone has their favoriate programming languages. I can understand that, and I can definitely see how PHP would be the ideal solution for some people and in certain situations.

              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
              • Comment removed based on user account deletion
                • But...but...I'm using standard RH9 distribution

                  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.

                  • Comment removed based on user account deletion
                    • No hard feelings but I still don't like the fact that PHP doesn't have an exact equivalent of perl's use Strict. I imagine spending hours trying to track down somebody else's variable typo (rather than actually problem solving) makes for a lot of fun all-nighters.

                      Anyway...
          • My guess would be for this part:

            while($row = mysql_fetch_assoc($resultset) {
            // Let's save the only two fields we need for later
            $languages[] = array($row['lang'] => $row['fullname']);
            }

            I'd gues you were really wanting to get:

            while($row = mysql_fetch_assoc($resultset) {
            // Let's save the only two fields we need for later
            $languages[$row['lang']] = $row['fullname'];
            }

            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

            • I think this all comes down to what you are used to. I code in C/C++, Java and a bunch of other languages. PHP was extremely easy to pick up.

              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.

              Object[][] s = new Object[10][1];
              for (int i = 0 ; i < s.length ; i++ ) {
              s[i] = new String[] {"foo","bar"};
              }

              When I saw that you could, in PHP,

              • I'll try to explain away any confusion I can for all the parts I know about in your message. I think most of the confusion comes from similar looking but different meanings for syntax between the languages. Ie, in java that empty square brackets [] are used in declaring an array while in PHP they mean to add whatever is on the other side of the assignment (=) operator to the end of the array and assign it to an index. So if the code you showed was from the documentation samples then it probably wasn't
                • <?
                  mysql_connect("gunnm","user","pass");
                  mysql_s elect_db("test");
                  $result = mysql_query("SELECT * FROM Languages");
                  while($row = mysql_fetch_assoc($result)) {
                  $LANG_LIST[] = $row;
                  }

                  foreach($LANG_LIST<B>[]</b&gt ; as $foo) {
                  echo $foo['lang']." ".$foo['fullname'];
                  }
                  ?>

                  produces the following output:

                  Warning: Invalid argument supplied for foreach() in /var/www/html/broken.php on line 11

                  the problem, it would seem, is using the brackets inside the foreach(). This version:

                  foreach($LA

                  • You are correct with the square brackets. I believe the empty square brackets are only used on the left side of an assignment operation. And the variable has to be either an array or not defined yet. I am a bit curious as to why you are loading everything into an array before outputting it? Usually you don't need to do that unless you want to do more processing on it later, but it seems like outputting is all that is being done. If you just needed output you can do:

                    <?php
                    // full php added so it will

                    • I am a bit curious as to why you are loading everything into an array before outputting it?

                      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

                      if (!isset($_SESSION('MENU')) {
                      // run mysql query to get menu
                      }

                      This is "caching" - the user only has to query the datab

                    • This is "caching" - the user only has to query the database for the lanuages menu data once per session. In ASP and some Java frameworks you can even store data in an application-scoped variable so everyone can share the data - but apparently PHP has no equivalent.

                      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
                    • The ADODB link should have gone to http://adodb.sourceforge.net/ [sourceforge.net]. Sorry about that.
                    • For your require problem, did you see any errors occur with the require? Did you put @ in front of the require call

                      ??? Why would I do that?

                      Yes, the first file had tags around the function.

                      It was probably the missing @, whatever that means (directive?).
                    • You probably wouldn't do that. The @ supresses any warnings or error output the function might produce. I was just trying to figure out how you could have a file be required, not see an error and still not get the function to be defined. Was everything in the same directory?
                    • Was everything in the same directory?

                      Argh...YES.

                      Look, if you're really interested, I'll take a look tonight and see if I can recreate the behavior.
                    • You seem to be getting upset about me asking some questions. I'm just trying to help figure out the problem. I know there's weirdness in include/require with the way the pathing works. The problem is the path is all relative to the original file, not the file that is doing the including. So if you had your a.php file in the web root, and b.php and c.php in an include directory, and had a.php include ("include/b.php"); and b.php did include("c.php"); it would be looking for c.php in the same place as a.p
                    • I'm interested in helping but only if you are still interested in finding out why the thing isn't working.

                      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.
                    • Excellent. It took me awhile (several weeks of hunting) one time when I had an include that included something else to figure out what the problem was and that the way PHP handles relative paths in includes is not very intuitive. I'm glad you didn't have to struggle with that at all.

                      I'd be interested in seeing the require code.
                    • Ok, as a sanity test, I did a small file like the following:

                      ******* funcs.php.inc ********

                      <?
                      $blah = "Hello world!";
                      function PrintHelloWorld() {
                      return "$blah<BR>";
                      }
                      ?>

                      ******* index.php ********

                      <?
                      require 'funcs.php.inc';
                      PrintHelloWorld();
                      ?>

                      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]

                    • Your sample index.php shouldn't output anything since $blah in that context isn't defined. If $blah="Hello world!"; was inside your function, it would work as expected, or if the first line of your function was

                      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
                    • I'd still have to recommend again that you use the full <?php tags.
                    • Not just "?"

                      I say again: wtf?
                    • Please don't take any of the following personally - I mainly venting my frustration with the crappy, inconsistent nature of PHP

                      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
                    • You can obviously do it however you want. There's no guarantee that whatever server you put your code on will support the short tags. If they have php they will support the long (<php) tags though.
                • That line that raises the first error is

                  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
  • 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!

    even gives you some useful collection objects like scripting.Dictionary.

    Unless that's very different to what I think it is then you want to look at array().

    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

    • 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!

      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
      • 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.

        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 Programmers can make any language suck.
    Bad Languages can make any programmer suck.
    • Ummmm...yes, I wrote that in my journal...it's buried, but it's there.

      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
      • I know, I was just pulling out the important bit for my own amusement.

        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. =)
        • ...Perl (which has switches to change it from "freakishly awful" to "exceptional" with a flip of a -t -w -s)

          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
          • Just for those playing along at home, adding the -s switch turns on strict syntax, which (among other things) forces your to declare your variables. The -w switch turns on all warnings, to fix those little things that might be big problems. And -t turns on "Taint Checking", which reminds you as much as algorithmically possible to sanitize user input (basically, it won't let you use a user-input variable for anything useful until you run it through a regexp, under the assumption that you'll use that regexp
            • Perl's basically, as far as I'm concerned, the Unix answer to VB (or vice versa)

              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...
              • It will indeed draw condemnation--but only from anti-Windows zealots and people who've never actually used VB. I used to complain about it, since my entire exposure to it had been a cheesy intro-to-business-programming class, but then I did some actual work in it a few years later, and it's surprisingly good for making quick gui prototypes and quick-n-dirty sysadmin tools.

"I'm a mean green mother from outer space" -- Audrey II, The Little Shop of Horrors

Working...