Parse error: syntax error, unexpected T_FUNCTION line 10?
The error is likely caused by
return preg_replace_callback($e, function($v) use ($s,$r) { return $r[$v[1]]; },$sql);
Chances are you're using PHP 5.2 or earlier, which doesn't support closures. You can find out which version of PHP you're using phpinfo()
.
You'll likely either need to upgrade to PHP 5.3+, or use create_function
, or write a static function and pass it as a callback.
Here's an example of the last option, using a simple class to store the state of $r
:
class My_callback {
public function __construct($s, $r) {
$this->s = $s; $this->r = $r;
}
function callback($v) { return $this->r[$v[1]]; }
}
function search_replace($s,$r,$sql) {
$e = '/('.implode('|',array_map('preg_quote', $s)).')/';
$r = array_combine($s,$r);
$c = new My_callback($s, $r);
return preg_replace_callback($e, array($c, 'callback'), $sql);
}
Parse error: syntax error, unexpected T_FUNCTION
I am a big fan of OO programming, so just for the fun:
class MyArrayOperations
{
private $base;
public function __construct(array $base)
{
$this->base = $base;
}
public function dif (array $vars )
{
$result = array();
foreach ( $this->base as $base )
if(!in_array($base, $vars))
$result[] = $base;
return $result;
}
$result = (new MyArrayOperations($allmodels))->dif($existmodels);
The class can be put in a separate file for reuse, and then just use the oneliner. And, of course the class can be extended with a kind of handy array operations.
Update
I realized that the oneliner only will work in php 5.4+, so for older versions use this:
$arrayops = new MyArrayOperations($allmodels);
$result = $arrayops->dif($existmodels);
Result in Codepad.
PHP, Parse error: syntax error, unexpected T_FUNCTION
Check your PHP version, anonymous functions are only supported on 5.3+
Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /home1/gcc/public_html/university/core/init.php on line 22
It seems because you are running on an old PHP version (older than PHP 5.3), anonymous function are seen as syntax errors. PHP implements callbacks as string. You can define the function normally, and pass a name of the function instead.
function custom_autoloader($class) {
// your code..
}
spl_autoload_register('custom_autoloader');
Parse error: syntax error, unexpected T_FUNCTION,
The problem is that you are attempting to register an anonymous function using spl_autoload_register()
, but as you told that you are using PHP5.2 on your web server.
Unfortunately PHP < 5.3 does not support anonymous functions. You need to write a "regular" function:
function my_autoload($class) {
require_once 'classes/' . $class . '.php';
}
spl_autoload_register('my_autoload');
This will work on PHP >= 5.3 as well.
syntax error, unexpected T_FUNCTION, expecting ')'
You need to change the way of calling function since anonymous functions are not supported in your PHP version:
function stack_2145($class_name)
{
$file_name = str_replace('\\', '/', $class_name);
$file_name = str_replace('_', '/', $file_name);
$file = dirname(__FILE__) . "/lib/$file_name.php";
if(is_file($file)) include $file;
}
spl_autoload_register("stack_2145");
Related Topics
Tilde Operator in Regular Expressions
How to Obtain a Nested HTML List from Object's Array Recordset
Get the Hierarchy of a Directory with PHP
How Does PHP Compare Strings with Comparison Operators
Multiple Replace (Probably Preg_Replace) of Same String with Array
Simplexml Get Attributes with a Namespace
How to Pass Parameters into a PHP Script Through a Webpage
Get Image Height and Width as Integer Values
How to Display "12 Minutes Ago" etc in a PHP Webpage
Determine If Business Is Open/Closed Based on Business Hours
How to Download Large Files Through PHP Script
Call PHP from Virtual/Custom "Web Server"
How to Select Default Value of a Field
MySQL Error When Inserting Data Containing Apostrophes (Single Quotes)
How to Obtain Anchor Part of Url After # in PHP
How to Get Data in Function Extend Controller