I was wondering what the gurus here at PHP Builder do when they start a new PHP script....

1.) Do you define variable types?

In Java you would do this...

int count;
int age;
String name;

Do you initialize your variables?

In Java you would do this...

count = 0;
age = 25;
name = "John Doe";

I realize that PHP isn't a true programmming language like C, C++, Java, etc. but I would still like to try and code like I am using one of those languages (and avoid "sloppiness").

Is that possible? Is it practical?

Amy

    PHP is loosely typed, so defining is not really done per se. I do initialize my variables.

      amy.damnit wrote:

      I realize that PHP isn't a true programmming language

      Oi! Says who?! :p

      As bastien said, PHP is "loosely-typed" - and by loosely, they practically mean un-typed. You don't declare variables as int's or char[]'s. You can force them to be one of the primitive types by a process called casting (whic you're probably familiar to).

      For more information, I would first suggest you take a look at this manual page: [man]language.types.type-juggling[/man]. From there, you'll find links along the left-hand nav menu to other sources for 'types' in PHP.

        bradgrafelman;10915268 wrote:

        Oi! Says who?! :p

        Most of the IT industry! 🙂

        As bastien said, PHP is "loosely-typed" - and by loosely, they practically mean un-typed. You don't declare variables as int's or char[]'s. You can force them to be one of the primitive types by a process called casting (whic you're probably familiar to).

        I am!

        So that was my next question... Should you "cast" variable to be thorough?

        For more information, I would first suggest you take a look at this manual page: [man]language.types.type-juggling[/man]. From there, you'll find links along the left-hand nav menu to other sources for 'types' in PHP.

        Thanks for the link. (Do you have the PHP Manual memorized?!) 🙂

        And, should you initialize all variables before you use them.

        I've personally never understood why the makers of PHP don't make it a strongly-typed language. It seems like they are getting closer to making PHP a real programming language with things like introducing OOP, yet loosely-typed - read no-type! - variables seem rather sloppy - read Microsoft-esque to me!! 😃

        Just my thoughts.

        Amy

          amy.damnit wrote:

          1.) Do you define variable types?

          Yes, by defining classes.

          amy.damnit wrote:

          In Java you would do this...

          That snippet of Java does not define any type. It merely declares a few variables.

          amy.damnit wrote:

          Do you initialize your variables?

          Yes.

          amy.damnit wrote:

          Most of the IT industry!

          We commonly call PHP a scripting language, but scripting languages are programming languages.

          amy.damnit wrote:

          Should you "cast" variable to be thorough?

          It depends. There is no point casting for the sake of casting.

          amy.damnit wrote:

          And, should you initialize all variables before you use them.

          Yes. Note that the PHP interpreter may emit a notice if a variable is used before it is defined.

          amy.damnit wrote:

          I've personally never understood why the makers of PHP don't make it a strongly-typed language.

          What do you understand by "strongly-typed language"? After all, I notice that you listed three "true" programming languages, of which C is regarded as weakly typed, C++ can also be regarded as weakly typed despite its more robust type system, while Java has loopholes in its type system that perhaps disqualify it from being really "strongly typed".

          amy.damnit wrote:

          It seems like they are getting closer to making PHP a real programming language with things like introducing OOP

          So, C is not a "real" programming language? After all, C does not natively support OOP through specific language constructs.

          amy.damnit wrote:

          yet loosely-typed - read no-type!

          PHP uses dynamic typing: the type of the variable depends on the type of the value that it stores or is otherwise associated with.

            laserlight;10915272 wrote:

            Yes, by defining classes.
            That snippet of Java does not define any type. It merely declares a few variables.

            It declares variable types.

            We commonly call PHP a scripting language, but scripting languages are programming languages.

            They are a subset of programming languages. A true "programming language" is compiled and is free standing in that is can run as its on .exe. A "scripting language" (e.g. PHP, VBA, etc) is only a bunch of "commands" that instruct an application envronment what to do. "Scripting languages" cannot exist if they don't have that environment. For instance, VBA cannot run without a Microsoft Access/Excel/Word application environment.

            It depends. There is no point casting for the sake of casting.

            That is what I figured.

            What do you understand by "strongly-typed language"? After all, I notice that you listed three "true" programming languages, of which C is regarded as weakly typed, C++ can also be regarded as weakly typed despite its more robust type system, while Java has loopholes in its type system that perhaps disqualify it from being really "strongly typed".

            That is an ENORMOUS topic and one that is often debated, however my main point is that regardless of what you consider to be a "strongly-typed language", PHP is definitely not one of them!! 🙂

            echo "Mary is $age years old, and has $checking_balance to her name."

            doesn't cut it as far as being "strongly typed". 🙂

            Strong vs weak typing characterizes whether type checking, static or dynamic, can be meaningfully circumvented. (I do not consider "casting" to be breaking any rules as it is done explicitly.) If you can't break the rules then it is a strongly typed language.

            Like I said, this is a big topic - especially when you add in Static, Dynamic and Manifest Typing - but it is clear that full programming languages like C, C++, Java, Pascal, C#, etc are much more "strongly-typed" than your PHP's and VBA's of the world.

            So, C is not a "real" programming language? After all, C does not natively support OOP through specific language constructs.[/quote}

            "C" is a "real" language because it can stand entirely on its own. OPP has nothing to do with what makes it a legitimate fully-functional programming language.

            A fully-functional programming language (e.g. Java) can be used to write the operating-platform on your cell-phone, or an application on your TV, or the apps that run all of the controls in the Space Shuttle. And languages like C and C++ can be used to write Operating Systems, or full applications like the Oracle DBMS or even the PHP interpreter!

            A scripting language has a much more limited scope and is entirely dependent on having a "host environment".

            It is not as much as better or worse, as different. Yet at the same time, scripting languages are clearly not as robust as a full programming language. After all, how many MRI's, Missle-Defense-Systems, DOPPLER Radar Systems do you know that were written with JavaScript, VBA, PHP, or a Shell Script?! 😃

            In the end, PHP is great for what it was designed, and it is superior to many others on the Web. However, it is what it is...

            Amy

              amy.damnit wrote:

              They are a subset of programming languages. A true "programming language" is compiled and is free standing in that is can run as its on .exe

              A PHP script can be compiled into it's own application; PHP-GTK, Roadsend PHP, ...

              Then again, since PHP has its own separate interpreter, you could just install PHP on a server, do "/usr/bin/php my_serivce.php &" and voila, you have your own daemon running.

                That may be so, however, PHP is still considered a "scripting" language by all accounts.

                (And it is clearly not in the same category as C, C++ or Java.)

                Regardless, for my web-based needs - for now - PHP is the right ticket, and I hope to become a PHP guru like many of you in short order! 🙂

                Amy

                  amy.damnit wrote:

                  It declares variable types.

                  No, the type names are part of the declaration of the variables.

                  amy.damnit wrote:

                  A true "programming language" is compiled and is free standing in that is can run as its on .exe. A "scripting language" (e.g. PHP, VBA, etc) is only a bunch of "commands" that instruct an application envronment what to do. "Scripting languages" cannot exist if they don't have that environment.

                  You are repeating Ousterhout's dichotomy. Going by your definition, Java is a scripting language, since its typical mode of interpretation is by a virtual machine, so Java programs are not free standing with respect to the machine itself. The point here is that interpretation always happens at some level.

                  amy.damnit wrote:

                  That is an ENORMOUS topic and one that is often debated, however my main point is that regardless of what you consider to be a "strongly-typed language", PHP is definitely not one of them!

                  I agree 🙂

                  amy.damnit wrote:

                  A fully-functional programming language (e.g. Java) can be used to write the operating-platform on your cell-phone, or an application on your TV, or the apps that run all of the controls in the Space Shuttle. And languages like C and C++ can be used to write Operating Systems, or full applications like the Oracle DBMS or even the PHP interpreter!

                  A scripting language has a much more limited scope and is entirely dependent on having a "host environment".

                  The problem is that you have confused the language with its typical mode of interpretation. I have no qualms about calling PHP a scripting language because its typical uses are associated with "scripting", but stating that it is somehow not a "real" programming language when it is Turing complete is another matter. So what if C can be used to write a PHP interpreter? PHP can be used to write a C compiler.

                    This topic(s) are obviously a "religious debate" - and we know how debates about "religions" end up - WARS!! 🙂

                    My general thoughts on what makes for a "serious" programming language/platform versus something that is more "recreational" is this...

                    • If you had a loved one who needed serious medical care (e.g. surgery) and their life was very dependent on software as part of the life-saving procedure, would you want the software written in VBA or something like C, C++, Java, etc?

                    • If you were in charge of a nuclear reactor's radioactive core, would you rather trust Windows Millenium Edition (or any version of Windows) or something like Unix?

                    • If you were in charge of ensuring that all trades for the day on the New York Stock Exchange were captured and easily recoverable, would you rather tust MS Access or something like a Mainframe, DB2 or Oracle?

                    What is "strong" and "weak" makes for great debates, but at the end of the day, it is pretty clear which technologies typically win out in "mission-critical" environments.


                    As for PHP, well...

                    • It is a scripting language
                    • It is not strongly-typed
                    • It is not anywhere nearly as sophisticated as C, C++ or Java
                    • It is limited in the environment in which it can currently run

                    however...

                    • It pretty much runs the Internet
                    • It is lean
                    • It is easier to learn than C, C++ or Java
                    • It runs larger sites like Wikipedia, Facebook and Yahoo
                    • It is supposedly (?) quicker than Java on the web in many cases
                    • It is open-source and doesn't require billion dollar infrastructure investments like JEE can.

                    As long as people are honest and don't make PHP something it is not then it can be a great tool in one's toolbelt!

                    But I digress!

                    Amy

                      would you code a web application in C? horses for courses. You comparisons are so broad as to be rendered meaningless.

                        dagon;10915371 wrote:

                        would you code a web application in C? horses for courses. You comparisons are so broad as to be rendered meaningless.

                        "Meaningless" is a rather strong term...

                        The point I was getting at was that scripting languages aren't in the same category as fully-functional languages like C, C++ and Java and so it doesn't make sense to try and equate them. They each have their purposes.

                        When you need a screwdriver, then use a screwdriver, but when you need a hammer, then you'd better use a hammer.

                        As to your question, if I was Amazon.com, yes, I would likely use C or C++ on the backend of my website!

                        Amy

                          amy.damnit;10915383 wrote:

                          The point I was getting at was that scripting languages aren't in the same category as fully-functional languages like C, C++ and Java and so it doesn't make sense to try and equate them. They each have their purposes.

                          only based on how you are choosing to define "fully-functional"

                            dagon;10915385 wrote:

                            only base on how you are choosing to define "fully-functional"

                            Are you saying that PHP can do everything that Java can? (Or did I misunderstand your response?)

                            Amy

                              amy.damnit wrote:

                              This topic(s) are obviously a "religious debate" - and we know how debates about "religions" end up - WARS!!

                              Yes, that is why I think that you should just drop this notion of "true" or "real" programming languages. There is nothing to be gained from such arbitrary classification.

                              amy.damnit wrote:

                              What is "strong" and "weak" makes for great debates, but at the end of the day, it is pretty clear which technologies typically win out in "mission-critical" environments.

                              Yes, but we are talking about programming languages. The associated technology (including how it is interpreted and suitable libraries and other tools) is important when considering the right tool for the job, but lacking the appropriate technology for certain tasks does not make a programming language any less "real", "true", or "serious".

                              EDIT:

                              amy.damnit wrote:

                              Are you saying that PHP can do everything that Java can?

                              Yes, when we talk about programming languages.

                                no, I'm saying you can't (ok shouldn't) post statements like :

                                "PHP isn't a true programmming language"
                                "The point I was getting at was that scripting languages aren't in the same category as fully-functional languages"

                                with out clarifying your definitions and point; especially on some place called PHPBuilder

                                but it sounds like this thread has fallen in to the semantic well so I'm off.

                                  Just a note: PHP is, in fact, a compiled language, not an interpreted language. The difference from something like C is that (in normal use) PHP is run-time compiled (as opposed to development-time compiled?). The last interpreted language I used was BASIC back in the late '80s. (Perl is another run-time compiled language.)

                                  You trade some overhead performance due to having to compile each script each time it is executed (assuming no buffering/caching/whatever mechanisms) in exchange for easy program portability. You also probably trade some run-time performance as the compiler is likely optimized for compilation speed versus executable optimization. But functionally I can see no reason to say that PHP is not a "real" programming language. Whether or not it is the best language for any given purpose is certainly debatable, but defining a language as real or not simply because you would not choose to use it for certain purposes is somewhat specious, to me. Is COBOL not a "real" programming language since you probably would not use it for creating a missile guidance system? Is Fortran not a "real" programming language because you probably would not use it for an e-commerce web site? Is Java not a "real" programming language because you probably would not use it to write Windows printer drivers?

                                    laserlight;10915388 wrote:

                                    Yes, when we talk about programming languages.

                                    Then why isn't it used by Corporate America on things like Trading Systems if it is equal to C, C++ or Java?

                                      amy.damnit;10915391 wrote:

                                      Then why isn't it used by Corporate America on things like Trading Systems if it is equal to C, C++ or Java?

                                      I don't think anyone is saying it's "equal" or "not equal"; simply that it is, in fact, a true programming language that has its uses (such as running these forums).

                                        NogDog;10915390 wrote:

                                        Just a note: PHP is, in fact, a compiled language, not an interpreted language. The difference from something like C is that (in normal use) PHP is run-time compiled (as opposed to development-time compiled?). The last interpreted language I used was BASIC back in the late '80s. (Perl is another run-time compiled language.)

                                        The same can be said of Java depending on your "interpretation"?! (pun intended) 🙂

                                        You trade some overhead performance due to having to compile each script each time it is executed (assuming no buffering/caching/whatever mechanisms) in exchange for easy program portability. You also probably trade some run-time performance as the compiler is likely optimized for compilation speed versus executable optimization. But functionally I can see no reason to say that PHP is not a "real" programming language.

                                        And thus the debate...

                                        Like most people I know, I don't consider a scripting language to be a fully-functional language. That doesn't make PHP any less good at what it does, but it clearly is not in the same category as C, C++, Java, or a lot of other programming languages.

                                        Whether or not it is the best language for any given purpose is certainly debatable, but defining a language as real or not simply because you would not choose to use it for certain purposes is somewhat specious, to me.

                                        It is a matter of classification. I think people take it too personally.

                                        HTML is not a "programming language" in my mind, yet it is a "markup language".

                                        Is COBOL not a "real" programming language since you probably would not use it for creating a missile guidance system?

                                        COBOL is a real programming language, and in fact, probably is used by the Dept of Defense more than you think. (Most of the BIG stuff in this world still runs on Mainframes.)

                                        Is Fortran not a "real" programming language because you probably would not use it for an e-commerce web site?

                                        FORTRAN is a real programming language, and in the past was used a lot by places like NASA. Is it a best choice for Amazon.com, well of course not since it isn't a web-based.

                                        Is Java not a "real" programming language because you probably would not use it to write Windows printer drivers?

                                        Java is a real programming language, and you could use it to write drivers.

                                        I don't know what to say. I think there is a very clear difference between scripting languages and regular programming languages. And while the line is blurred as PHP adds more things like OOP, ther is still a pretty clear division.

                                        There are probably lots of people I know who could do a better job explaining the true differences - although I thought I did okay - but I never knew this was a debatable topic? 😕

                                        I know a young girl who dropped out of high school who is convinced that getting a GED online is exactly the same as sitting in a classroom and graduating from high school with her peers. To her there is no distinction, but to everyone else it is clear that they are not the same thing.

                                        Fortunately, using PHP never hurt anyone! 😃 And, if you want to build really cool websites like I do, then it is one of the better choices. (Even though it is still just a scripting language.)

                                        (I used to get harassed - to no end - by Microsoft people on another usergroup who were CONVINCED that MS Access and SQL Server were 100% EQUAL to something like DB2 and Oracle. What can you say in those cases???)

                                        Anyways, I'm not taking any of this personally or spinning my wheels, so I sure hope that no one else is...

                                        Amy
                                        🙂