In my project, when I try to install a software, I got an parse error in last step of installation
The parse error is
Parse error: syntax error, unexpected '#' in
/path/to/server/subfolder1/projectfoldername/subfolder/filename.php
on line 21
The coding in that particular line of that file is
if(#constant($matches[1][0]) != #$matches[1][0]){
if(!empty(#constant(#$matches[1][0])) & !empty(#$matches[0][0]) & !empty(#$design_m_r[$key])){
$design_m_r[$key] = #str_replace($matches[0][0], constant($matches[1][0]), $design_m_r[$key]);
}
}
Our site php version is php 5.3.28. I tried to search for this error. But I dont get any solution for this. Some of the forums told about this error as "This is the advanced php version functions. So this should not support for php 5.3.28 version". But when I searched, there is no versions used this type of function.
You can't use the # error suppression operator like that.
From the PHP Docs..
The #-operator works only on expressions. A simple rule of thumb is:
if you can take the value of something, you can prepend the # operator
to it. For instance, you can prepend it to variables, function and
include calls, constants, and so forth. You cannot prepend it to
function or class definitions, or conditional structures such as if
and foreach, and so forth.
Also, passing arbitrary expressions on empty is allowed only from PHP 5.5 .
Related
I have a case with a simple syntax error.
It goes like this
class Foo
{
public function __construct($a,$b,$c,) {...}
So, there is an syntax error after param $c -- an excess comma (im using php7.3).
When i run psalm
./vendor/bin/psalm src/Foo.php --no-cache it shows that nothing is broken here.
My question is am i missing some config or something else, why do psalm do not catch that simple syntax error case?
With php8 this code no longer brings errors:
<?php
class Foo
{
public function __construct($a,$b,$c,) {}
}
Try it self.
I suspect psalm may already take that into account.
The decisive factor is which errors PHP itself delivers and not what any IDE or other tools display.
Parse errors Psalm reports are generated by nikic/php-parser which Psalm uses under the hood, and the parser itself does not distinguish between PHP 8 and PHP 7 syntax.
The only reason Psalm reports parse errors at all is that it can't understand syntactically incorrect code. It's not Psalm's goal to find any syntax errors, there are a lot of tools that do that.
Thus you should be using a proper syntax checker, either php -l directly or some wrapper like php-parallel-lint/php-parallel-lint.
I have theses errors in php v7.2 but don't see any E_WARNING when using php v7.1.
How can I resolve following errors?
/web13/web/boutique/includes/Sites/Shop/NavigationHistory.php on line 39
[12-Jan-2018 22:44:20 America/Toronto] PHP Warning: Use of undefined constant MODULE_HEADER_SELECT_TEMPLATE_STATUS - assumed 'MODULE_HEADER_SELECT_TEMPLATE_STATUS' (this will throw an Error in a future version of PHP) in /var/www/clients/client1/web13/web/boutique/includes/Sites/Shop/Template.php on line 356
This is a common warning that occurs whenever PHP has detected the usage of an undefined constant.
Here is an example of constant being defined in PHP:
define('PI', 3.14);
Below is a list of some cases that might cause the issue:
Forgetting to use a $ symbol at the start of a variable name.
$name = "Aniket";
echo name; // forgot to add $ before name
The above code will throw: Notice: Use of undefined constant name – assumed ‘name’. Because there is no dollar sign in front of the variable “name”, PHP assumes that I was trying to reference a constant variable called “name”.
Forgetting to place quotes around strings.
echo $_POST[email];
In the example above, I failed to place quotes around the $_POST variable “email”. This code will throw: Notice: Use of undefined constant name – assumed ’email’.
To fix this, I’d obviously have to do the following:
echo $_POST["email"];
According to Deprecated features in PHP 7.2.x you should not use undefined constants because:
Unquoted strings that are non-existent global constants are taken to be strings of themselves.
This behaviour used to emit an E_NOTICE, but will now emit an E_WARNING. In the next major version of PHP, an Error exception will be thrown instead.
You can prevent this E_WARNING only if you declare the constant value before using it.
In the above question, MODULE_HEADER_SELECT_TEMPLATE_STATUS is not defined.
In addition, for those who are new to wordpress Plugin development and cant seem to figure out what it means to define "Constant" before its used...
Here is an example of what will throw an error:
add_action('wp_enqueue_scripts', myprefix_load_styles);
Declaring a function directly like that in a hook is one way to generate an error like this.
Instead:
add_action('wp_enqueue_scripts', 'myprefix_load_styles');
Note, the function name is now inside the quotes.
And that should work, if thats your scenario.
I've just created a PHP job to recursively clean up all files in a PHP project and automatically quote all strings which are undefined constants used inside square brackets for the array syntax.
Observation: this fix only targets array usage like $a[key1] which will be automatically transformed into $a['key1']. The clean up process DOES NOT parse and compute a list of defined constants in your project, in order to white-list them for usage without quotes in all possible contexts.
It's recommended to run it for your project on DEV first, check functionality and then push to LIVE.
QUICK USAGE:
git clone https://github.com/eyroot/lx-utils lx-utils
cd lx-utils && composer install --no-dev
php run/cleanUpSquareBrackets.php /path/you/want/to/clean/up
The complete usage instructions and source code are on the page:
https://github.com/eyroot/lx-utils
Short hand Search replace Regex for Notepad++
for array(test=>'bla')
Search: ([ (\t])([aA-zZ]+)([\t ]*=>)
Replace: \1'\2'\3
for [test]
Search \[([aA-Zz]+)\]
Replace:['\1']
I had a similar error in WordPress using PHP version 7.3:
The error: [23-Dec-2022 21:53:48 UTC] PHP Warning: Use of undefined constant ‘xmlrpc_enabled’ - assumed '‘xmlrpc_enabled’' (this will throw an Error in a future version of PHP) in functions.php in a child theme
The code:
add_filter( ‘xmlrpc_enabled’, ‘__return_false’ );
as you can see, there were quotes that in my editor (gedit) look like inverted commas. These do not work.
After I changed the quotes to:
add_filter( 'xmlrpc_enabled', '__return_false' );
the errors went away.
Thank you #wale for the pointer on where to look.
This snippet of code causes a PHP fatal error in 5.6, but works fine in 7.0 and above. There is no documented change to isset that I could find stating that it works with arrays as constants.
<?php
class Test
{
const A = [1];
}
echo isset(Test::A[0]);
Does anyone know of any documentation stating this was an actual change? Is it safe to use isset with arrays as constants?
PHP 5.6.30 error:
Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
isset() is a language construct and not a function, so perhaps this paragraph (from http://php.net/manual/en/migration70.incompatible.php) applies:
PHP 7 now uses an abstract syntax tree when parsing source files. This
has permitted many improvements to the language which were previously
impossible due to limitations in the parser used in earlier versions
of PHP, but has resulted in the removal of a few special cases for
consistency reasons, which has resulted in backward compatibility
breaks.
Hi i have a new server runing php 5.4 on rackspace and there is errors con my code
i cannot do this becouse its giveme a error:
if(empty($basics->conversions*100) || empty($basics->activations))
this is the problem: ($basics->conversions*100);
i have to do $vr = $basic->conversions*100;
if(empty($vr)) but i have this all over my code and i cannot fix it
Parse error: syntax error, unexpected '*', expecting ')' in ***
the other error is when using a function returning an array and accesing that array insted of assign it to a variable example:
getReports($date, $todate)['utm'];
this gives me error.
but if i do:
$arrReportes = getReports($date,$todate);
$arrReports['utm'];
Works perfectly why ? can you helpme i cannot find any on google
From http://php.net/manual/en/function.empty.php
Prior to PHP 5.5, empty() only supports variables; anything else will
result in a parse error. In other words, the following will not work:
empty(trim($name)). Instead, use trim($name) == false.
This explains why your first error is occurring - empty() just doesn't let you pass an expression to it like you are doing in the version of php you said you are running (5.4).
The second error should really have been put in a separate question. In theory what you are trying to do should be possible since php 5.4 -
As of PHP 5.4 it is possible to array dereference the result of a
function or method call directly. Before it was only possible using a
temporary variable.
(from http://php.net/manual/en/language.types.array.php)
- but you claim to be running 5.4 so I'd imagine there's an issue somewhere else. I'd double check the version of php you're running, and ensure your code isn't at fault there too. You didn't specify what error you were getting, so it could well be that the returned array is null, or anything.
I have a plugin in my forum which throws a warning.
I want to fix the problem, but firstly, I want to hide the warning message to the users.
I know I can change it globally, but I just want to do it for some lines.
How can I do this?
$bbcode_parser =& new vB_BbCodeParser($vbulletin, fetch_tag_list());
gives the error:
Warnung: Assigning the return value of new by reference is deprecated in ..../includes/garage_func_var.php (Zeile 6411)
I already know I need to use # but where do I put this?
# can be used to suppress warnings, notices and errors.
Fatal errors are displayed in PHP 7 which breaks the script.
# can be used before variables, functions, include calls, constants and so forth but cannot be prepended to function or class definitions, conditionals, loops and so forth.
So for example, to hide an undefined property error:
Class Cars{
}
$obj = new Cars();
echo #$obj->idontexist;
As to your specific problem:
#$bbcode_parser =& new vB_BbCodeParser($vbulletin, fetch_tag_list());
should fix it.
While the mentioned deprecated warning message is displayed in PHP 5, the following will be displayed in PHP 7 since it was deprecated in the upgrade.
PHP 7 Note:
Parse error: syntax error, unexpected 'new' (T_NEW)
Use # only as an absolute last resort. It is considered bad coding to use # as slows things down and moreover creates headaches for programmers (including yourself) in the future when trying to debug. This is really frowned upon.
You should:
Hide warnings only for that call using a function using set_ini
Use "try" in php to manage errors.