2

For C/C++, I think the Preprocessor does the Macro processing before compiler compiles the program and that's why Macro name has to be literal or absolute, no variable allowed to declared Macro name.

In case of PHP, we can declare like this: DEFINE($name, $value); Which means, interpreter first interprets the variable and then macro is defined, i.e. like runtime macro processing.

Which actually creates a doubt that, is that really macro processing ? If not then what's the purpose of introducing such keyword like 'define'; to have values which cannot be changed ?

Or if there's macro processing, then can anybody explain, how it could be working ?

4 Answers 4

5

For the sake of completeness, there is a constant that is defined at compile-time :

const MY_CONSTANT = 'something';

As opposed to defining constants using define(), constants defined using the const keyword must be declared at the top-level scope because they are defined at compile-time. This means that they cannot be declared inside functions, loops or if statements.

From the PHP Manual.

This is for the current PHP Version, 5.3.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually I mixed up the const and define and assumed that define would be working like C macro. Anyway, thank you for the link ;).
4

Define() declares a constant. In PHP you cannot declare a macro like in C and generally there is no need to.

For more info try the excelent php website

Comments

1

PHP is an interpreted language - we can not talk about compilation in PHPs case. DEFINE is used for defining constants - PI for example

1 Comment

php.net/manual/en/function.pi.php, and sure interpreter languages are compiled as well.
1

http://php.net/manual/en/function.define.php

Defines a named constant at runtime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.