Asked  7 Months ago    Answers:  5   Viewed   29 times

I'm writing a small repository for my little app team's Java code, and I have this error all over my code.

$base = explode(".", $class)[0];

The problem occurs only with this one line of code, every time. As far as I know, the above is correct PHP syntax, so what's going on?

Parse error: syntax error, unexpected '[' in .../mitc/code/index.php on line 27

If you'd like to see the error, it's at http://chancehenrik.x10.mx/mitc/code/ and elsewhere on my site.

 Answers

16

That's called array dereferencing and only works in PHP 5.4+. You're probably running PHP 5.3.x wherever you are getting that error.

See results based on different PHP versions

Wednesday, March 31, 2021
 
Oshrib
answered 7 Months ago
95

Memory usage is 635384 bytes before calling query. I'm guessing query allocates in chunks, for each record.

Ding ding ding!

When connecting to MySQL, PHP likes using buffered queries. This is true regardless of what method you're using to connect. When using buffered queries, the entire resultset is fetched immediately instead of being fetched when you ask. This is usually good for performance, as there are fewer round-trips.

But like everything in PHP, there's a gotcha. As noted on the buffering page:

When using libmysql as library PHP's memory limit won't count the memory used for result sets unless the data is fetched into PHP variables. With mysqlnd the memory accounted for will include the full result set.

You're using PHP 5.3, which means that there's a good chance that you're using mysqlnd.

You'll want to turn off buffered queries here. It's done differently in every PHP interface to MySQL:

  • For PDO, you'll need to set the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute to false.
  • For mysqli, you need to pass the MYSQLI_USE_RESULT constant to the query method.
  • For mysql, you need to call mysql_unbuffered_query instead of mysql_query.

Full details and examples are on the page.

Big fat unbuffered query gotcha!

You must properly close the statement handle and free the result set before issuing another query:

  • In PDO, this means calling closeCursor on the statement handle.
  • In mysqli, this means calling free_result on the statement handle or free on the result handle, depending on what you're working with.
  • In mysql, this means calling mysql_free_result

Failure to do this will result in an error.

Saturday, May 29, 2021
 
letrollpoilu
answered 5 Months ago
93

Please try this

$str = "field1.id as field1, 
       DATE_SUB(field2, INTERVAL (DAYOFMONTH(field2)-1) DAY) as field2,   
       field3.name as field3";

$buffer = '';
$stack = array();
$depth = 0;
$len = strlen($str);
for ($i=0; $i<$len; $i++) {
    $char = $str[$i];
    switch ($char) {
    case '(':
        $depth++;
        break;
    case ',':
        if (!$depth) {
            if ($buffer !== '') {
                $stack[] = $buffer;
                $buffer = '';
            }
            continue 2;
        }
        break;
    case ' ':
        if (!$depth) {
             $buffer .= ' ';
            continue 2;
        }
        break;
    case ')':
        if ($depth) {
            $depth--;
        } else {
            $stack[] = $buffer.$char;
            $buffer = '';
            continue 2;
        }
        break;
    }
    $buffer .= $char;
}
if ($buffer !== '') {
    $stack[] = $buffer;
}
echo "<pre>";
print_r($stack);
echo "</pre>";
Saturday, May 29, 2021
 
Asher
answered 5 Months ago
53
$exploded = explode('.', 'a.string.to.explode');
$exploded = array_combine(range(1, count($exploded)), $exploded);
var_dump($exploded);

Done!

Saturday, May 29, 2021
 
edorian
answered 5 Months ago
40
$expires = time()+604800

in the function definition.

Default value can't be the result of a function, only a simple value QUoting from the manual:

The default value must be a constant expression, not (for example) a variable, a class member or a function call.

Use:

public function set($name,$value,$expires = NULL) { 
    if (is_null($expires))
        $expires = time()+604800;
    echo $expires; 
    return setcookie($name, $value, $expires); 
} 
Saturday, May 29, 2021
 
phirschybar
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :