[Solved] How is PHP a Scripting Language when it’s written in C?


To start at the beginning you need to understand that features like object orientation don’t make a language more powerful. There is nothing that can be done with an object oriented language that can’t be done in Assembler. Object orientation is just a different way of writing things.

So for example, if you have a class like this (using Java here, but it’s the same for any other object orientated language):

class Foo {
    int x,y;
    int getSum(int z) {
        return x+y+z;
    }
}

That is equivalent to

struct Foo {
    int x, y;
}
int getSum(Foo foo, int z) {
    return foo.x+foo.y+z;
}

So here you have some code that does exactly the same as the code above, but is not object oriented. So any compiler or interpreter that compiles or interprets object oriented code translates that code into something the machine understands by creating code that does the same as the object oriented code, but without the object orientation. Furthermore it actually translates it into machine code that does the same thing that the original code did.

@the second point (why is PHP interpreted while c is not):
Think of the PHP interpreter as a program, written in c, that just walks over your code and executes whatever the code does. What comes next is greatly simplified. Take that example (it’s once again in Java, but again, it’s about the same in any language):

String phpCode="print('Hello World');exit();";
String[] tokens = phpCode.split(";").strip();
for (String token : tokens) {
    if (token.startsWith("print")) {
        String param = phpCode.replace("print(","").replace(")","");
        System.out.println(param);
    } else if (token.equals("exit()")) {
        System.exit(0);
    }
}

What this does is it interprets the content of phpCode. It currently understands the keywords print and exit. An actually usable interpreter is much more complicated, but that’s the basic concept. It runs over the code, splits it into pieces that it can understand and then executes code that does what the interpreted code should do.

0

solved How is PHP a Scripting Language when it’s written in C?