This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Can you undefine or change a constant in PHP?
(6 answers)
Closed 5 years ago.
I am trying to define a session variable as constant ...
define("MY_VAR", $_SESSION['variable_name']);
I start the session before accessing this constant
However, when I try to change the value of the variable ...
MY_VAR = "xyz"
... I get following message
"Parse error: syntax error, unexpected '=' in"
I am not sure what's happening here. It would be great if someone could shed some light on this.
Note: Instead, if I use $_SESSION['variable_name'] at all places where I used MY_VAR, the code works fine
The point of constants is they are constant.
Once set you can't change them.
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 2 years ago.
after establishing the connection I'm getting this kind of error in my PHP extension file. I have also tried in otherways but I get this kind of error.
You just mis-spelled "require".
Use any of these:
require 'path/to/script.php'
require('path/to/script.php')
require_once 'path/to/script.php'
require_once('path/to/script.php')
Look for the word "reqiure" in the code and replace it with require.
You are getting this error because the PHP parser does not expect that string token at that point in the syntax.
From the documentation, this is what a T_STRING is:
identifiers, e.g. keywords like parent and self, function names, class names and more are matched. See also T_CONSTANT_ENCAPSED_STRING.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
instantiate a class from a variable in PHP?
(5 answers)
dynamic class names in php
(7 answers)
Closed 2 years ago.
I am trying to make a small script that will load php files and then initiate classes automatically. I am trying to do it like this,
$className = ucfirst($folderName).'()';
\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor\$className );
//HERE I AM GETTING ERROR BECAUSE OF THE $class VARIABLE USED.
The error I get:
Parse error: syntax error, unexpected '$className' (T_VARIABLE), expecting identifier (T_STRING)
How can I get ride of the error and initiate the class?
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
hello in my PHP file i got error on host for this line that defines const array:
const telegram_methods=['sendMessage'=>'sendMessage','answerCallbackQuery'=>'answerCallbackQuery','forwardMessage'=>'forwardMessage'];
after some changes in host configurations. the error is:
Parse error: syntax error, unexpected '[' in /........./st_datas.php on line 8
what is the problem of this line or host configurations?
and this post:
PHP Parse/Syntax Errors; and How to solve them?
is so general and not duplicate of my question.
Maybe for your php version. If your php version >= 5.4, you can use brackets to define array.
So try it:
const telegram_methods = array('sendMessage'=>'sendMessage','answerCallbackQuery'=>'answerCallbackQuery','forwardMessage'=>'forwardMessage');
const telegram_methods=array('sendMessage'=>'sendMessage','answerCallbackQuery'=>'answerCallbackQuery','forwardMessage'=>'forwardMessage');
In regards to your follow up question (arrays are not allowed in class constants) it means simply as it says.
This has been updated in PHP 7 but before then if you need to set a class variable as an array simply drop the constant.
Check out the comments here for a more detailed response.
http://php.net/manual/en/language.oop5.constants.php
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I'm a PHP beginner and I have a problem: when I use this:
if(isset($_POST['doc_lang'], $_POST['doc_title'], $_POST['doc_header'], $_POST['doc_body'], $_POST['doc_footer']) AND !empty($_POST['doc_lang'], $_POST['doc_title'], $_POST['doc_header'], $_POST['doc_body'], $_POST['doc_footer']))
I have this error: Parse error: syntax error, unexpected ',' in (...)/WebDoc.php on line 3.
What is the correct syntax? Thank
You need a proper logical operator so depending what you are trying to do, it could for example be || or &&
To the best of my knowledge, empty() cannot accept multiple variables the way that isset() does.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I'm having a problem in my script class, is giving me the error:
Parse error: syntax error, unexpected '$client' (T_VARIABLE), expecting identifier (T_STRING)
But I can't figure out what is the problem with the "client Variable".
While accessing constant class members you need to use self::, but while
accessing local variable you can use simply by variable name.
So you need to use self::client instead of $client. Because $client is constant class member.
DEMO or See More Info