Search code examples
phpapachecentosconfigquote

Configuration quotes on variables in PHP


I'm new on the site (hi!) and I had a problem that I can't resolve yet. I look for it, but no results... I think it's a stupid thing, but I can't find the solution.

I'm migrating a system (PHP) to other server (both are CentOS) and I have a problem with the quotes in PHP variables:

    Example:
    --------
    $_GET[var]
    $db_reg[assoc]
    $array[value]
    define(NAME,'value')
    etc..

All cases can fix adding quotes to indexes, but I have thousand of PHP files, with millions of lines each one, and I can't check one by one... it will take around two lives and a half.

In the old server, it works normally, but in the new, the variables are not recognized, shows a PHP notice:

"Notice: Use of undefined constant XXXX - assumed 'XXXX in..." (ej: $_POST[XXXX])

Is there a configuration in Apache or PHP for recognizing or not (indistinct) quotes on variables?

The PHP version on both servers are the same, and I have checked the php.ini file, and they are similar.


Solution

  • I guess you need some information about variables and their usage (when working with arrays) and fixed index concretions.

    Usage of variables:

    $array = array('hi', 'you', 'there');
    $i = 1;
    echo $array[$i]; // -> Works and is **fine** (will output 'you')
    
    // --------------------------------
    
    $array = array('a' => 'hi', 'b' => 'you', 'c' => 'there');
    echo $array['a']; // -> Works and is **fine**
    // Note: in especially that case **NEVER** use barewords like this: echo $array[a] for >> a << here is expected to be a constant which (mostly) does not exist!
    // PHP is so fuzzy that it will mostly interpret around like insane and output you the desired value ('hi' in that case), but that was never meant to be! And should always be prevented. That's the reason for you getting that notice...
    
    // So never do this:
    echo $array[a];
    // ... if >> a << is NOT a valid and available constant
    

    Important (summed up to the point):

    Words without quotes and the dollar are considered constants which you perhaps do not use very often. In most cases, you use quotes (like described above) or real variables (identified by $) as index to access data structures/arrays.