Syntax error when trying to put together constant and string [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
Normally i wouldn't post syntax errors here, but this case is a bit suspicious to me. I was trying to put together a constant with a string. I works fine on localhost, but when i push my project on my web server, it tells me:
Parse error: syntax error, unexpected '.', expecting ',' or ';' in xxx on line xxx
Code:
define("ROOT", str_replace("index.php", "", $_SERVER["SCRIPT_FILENAME"]));
and
$somePath = ROOT . "some/path"; //Synax error in this line
Has this something to do with different PHP versions? (5.5 on webserver, 7.0 localhost)
Update
Some updates in the code:
class SomeClass {
private $somePath = ROOT . "some/path"; //Synax error in this line
...
}

You can't use str_replace() nor any function when defining a contant. Try:
define("ROOT", $_SERVER["SCRIPT_FILENAME"]);
$somePath = str_replace("index.php", "", ROOT) . "some/path";
Please refer to PHP Manual:
In PHP 5, value must be a scalar value (integer, float, string, boolean, or NULL). In PHP 7, array values are also accepted.

Related

PHP function giving error in my system [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I don't know why the code below giving an error on my laptop while not at my friend's.
<?php
function myfunction() : int {
return 10;
}
echo myfunction();
?>
Error
Parse error: syntax error, unexpected ':', expecting '{' in (my location) on line 2.
If I remove the ": int" on line 2 everything is fine, but can someone explain why this code can't run on mine?
Please read the documentation. It is a PHP7+ only feature.
What might be a good idea as a work around, until you migrate to PHP7, is to do the following:
function myfunction() {
return (int)10;
}
var_dump(myfunction());
That will convert the return to an integer.
It's worth noting, this won't throw any warnings if the return value cannot be resolved.
I.E. If you passed parameters and those parameters were letters in a string, for instance, you'd get Warning: A non-numeric value encountered. However, for now, I think the above solution will suffice.
I strongly recommend upgrading to the latest version of PHP, though.

how use strings with $ (not var) in PHP [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I place to the php line a large piece of html code with javascript inserts (with jQuery) in which $ symbols is present.
<?php
$site = <<<SITE_CODE
setTimeout(function(){$(g_utils._f().menu.current_id).trigger('click')}, g_utils.effects.animation(g_utils._f().animation.events.loading, 'delay2'));
SITE_CODE;
echo $site;
?>
PHP takes this as a variable and produces an error.
Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$'
in D:\site\index.php on line 328
Tell me how to get rid of this problem?
If you single quote the EOL string it will not evaluate any variable:
$site =
<<<'SITE_CODE'
setTimeout(function(){$(g_utils._f().menu.current_id).trigger('click')}, g_utils.effects.animation(g_utils._f().animation.events.loading, 'delay2'));
SITE_CODE;

PHP: xpath not working on php5.3 because of unexpected "[" [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I was running the following line on PHP5.4 without any problem:
$lstContent = $data->xpath("/response/lst[#name='highlighting']")[0]->lst[$i]->arr->str;
But now on PHP5.3 (production system) I get the following error:
Parse error: syntax error, unexpected '[' in /var/www/html/upload/inc_suche_code.php on line 153
Any ideas for a quick fix?
Updating PHP won't work for me.
In older versions of PHP you can not access array values directly on variables that are the result of a function. You have to split up the expression using a temporary variable.
$result = $data->xpath("/response/lst[#name='highlighting']");
$lstContent = $result[0]->lst[$i]->arr->str;
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.
Source: http://php.net/manual/en/language.types.array.php
Edit: Obligatory "you should also consider upgrading your PHP version". This annoying limitation was fixed ages ago, not to mention that the 5.3 had its end of life in 2014, meaning it has not received security upgrades since.

Concat Constant in php [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
namespace my;
const DHA=__NAMESPACE__."you are accessing constant"; //generate parse error
const DHA=__NAMESPACE__,"you are accessing constant"; //generate parse error
I tried concating using '.' and ',' but both it gives
Parse error: syntax error, unexpected '.', expecting ',' or ';'
EDIT (UPDATE) : This is a bug in PHP 5.5. It has been resolved in 5.6. https://wiki.php.net/rfc/const_scalar_exprs.
Which version of PHP
See https://bugs.php.net/bug.php?id=42355
Considered a new feature in 5.6
I have 5.5.9 right now and get the same results as you do

unexpected 'class' (T_CLASS) only on remote (not in local) [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
We are developping a CRM.
In local, I have no problem, but in remote (OVH), I have this error message :
Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /home/dubinfo/www/CRM/model/Locataire.php on line 126
This is the code :
public function setVisites($visites) {
$this->_visites = CheckTyper::isArrayOfModel($visites,
VisiteMaisonInvestisseur::class, 'visites', __CLASS__);
}
The version of PHP on remote host (OVH) is 5.4.38
Using class as a name of a constant is available in PHP 5.5 only.
To get the class name you can replace VisiteMaisonInvestisseur::class with get_class(new VisiteMaisonInvestisseur).
Or change the name of the constant. For example: VisiteMaisonInvestisseur::class_name.
The problem is with VisitMaisonInvestisseur::class. class is a reserved word in PHP, so you can't use it as the name of a constant.
If it works on your local server, it must be version-specific. But I've tested this in 5.3 and 5.6, and they both report an error for Classname::class.

Categories