PHP is calling error on code that in not being run? - php

I was troubleshooting some code and I took it out of the execution path so that I could look in other places.
PHP continued to call out the code as an error despite it not being executed. It is in a function that is not called.
Is this expected behavior?
// not in execution path
$temp1 = $this->hash;
$temp = array('1', $temp1); // this is line 121
$this->database_object->_pdoQuery('none', 'update_picture', $temp );
syntax error, unexpected T_LNUMBER, expecting ']' in
/home/foo/public_html/dev/foo/source/class.MAppPicture.php
on line 121

The entire file has to be parsed prior at the time of access or include. This is generating an error because it is a parse time error and php can't understand the file as a whole because of the syntax error.

Related

XAMPP PHP Named Arguments gives " syntax error, unexpected ':', expecting ')'"

In an include file I declare a function
function patch_163_output_row_header(
string $title,
string $label,
$help,
bool $read_only=false,
bool $required=false,
string $label_cell
)
and when I compile this using the XAMPP PHP compiler (8.1.6 as far as I can tell) it is OK
In another file I try to use the function
patch_163_output_row_header(
title:$title,
label:$label,
help:$help,
read_only:$read_only,
required:$required,
label_cell:$label_cell
);
when I try to compile I get the error mentioned above on the"title" line.
I can see no obvious issues - title does not seem to be a reserved word?
The strange thing then is that these two files are part of a website and when they are executed by the same XAMPP stack (presumably using the same PHP compiler?) there are no errors reported and they work.

stop checking code syntax after some command?

echo $a;
exit();
die();
eecho $b; // line 40
Error - syntax error on line 40...
Why exit or die don't work in such a case?
How to stop checking code syntax after some command?
One method you can use is to comment the rest of the code.
echo $a;
/* Comment rest of the file
exit();
die();
eecho $b; // line 40
Or if you want to "enable" the code on line 41 again you put */ to stop the comment.
You don't have to put a */ in the code if you only use it for debugging (as I do).
It will create an notice:
Warning: Unterminated comment starting line 4 in /in/jkqu0 on line 4
But as long as it's only debugging then it's no harm done.
https://3v4l.org/jkqu0
eecho $b; ????
Because php script execution works that way. php parser first check for php syntax in the script if it found any error it throws it before interpret it.
Steps of php script execution.
Lexing
Parsing
Compilation
Interpretation

echo -e content of file fetched using foreach

Forgive me, I'm not a programmer.
I am trying to include the last lines of a log file on a PHP page with two line breaks after each line read. Using responses from other questions, I came up with the following:
$file = "error.log";
foreach(file($file) as $line) {
echo -e $line. "\n\n";
}
When I execute this on the command line using php file.php, it is displayed correctly with the line breaks, however, on a webpage, it ignores the line breaks and prints a dirty output.
I tried to use echo -e in place of echo, but that doesn't even present the dirty output, but instead throws an error:
PHP Parse error: syntax error, unexpected '$line' (T_VARIABLE), expecting ',' or ';' in /var/www/err.php on line 12
I'm assuming echo -e isn't allowed within the foreach function? What am I doing wrong and how can I achieve the desired results?
echo in PHP is different than the echo command line in linux. You can't pass arguments to echo in PHP like that.
try :
echo nl2br($line . PHP_EOL);
Or if you only care about the browser
echo $line . "<br>";

Embedding PHP inside PHP

I have recently put up a server, and I am making changes gradually. One of the changes is the contents of a php page. I commonly use a php "shell" (just a textarea and the php just evals what is written). I have made some changes and want to use
fwrite($file, "<?php php here ?>");
but i am getting an error. I believe I know the problem, and it is because i have nested php like
<?php
$a = fopen('../php.php', 'w');
fwrite($a, "
<?php
eval($_POST['phptorun']);
?>
");
fclose($a);
?>
Is there any way I can get it to just put that php code into the file php.php?
The error i was getting is:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/html/Tyler/replace.php on line 5
Edit:
I forgot to say a couple of things. The server is a LAMP (Linux Apache MySQL PHP), running on my Raspberry Pi 2, model B. If you want to visit my server, it is at 71.204.114.18
Try to escape the $ symbol:
<?php
$a = fopen('../php.php', 'w');
fwrite($a, "
<?php
eval(\$_POST['phptorun']);
?>
");
fclose($a);
?>

Unexplained syntax error with logical OR ( || )

I'm working with NetBeans for Mac, and I'm running CakePHP (though I don't think the framework has anything to do with it) in a shared hosting in Linux. This is not a big issue, but it is frustrating.
I wonder why I can't simply do this:
if($this->Session->read('User.value1') || $this->Session->read('User.value2')){
...
}
The error message I get is:
Error: syntax error, unexpected '$this' (T_VARIABLE)
Why is there a syntax error? I can't see it.
I can do this with no problems:
if($this->Session->read('value1')){
...
}
I can also do this with no problems (no whitespace around ||):
if($this->Session->read('User.value1')||$this->Session->read('User.value2')){
...
}
But if I put spaces around the || operator, it stops working. Or rather — and this is the most confusing part — sometimes it stops working when I put spaces around the || operator, and sometimes it doesn't.
I thought this might be a bug in Netbeans 7.4, but when I ignored the warning from NetBeans and tried to run the code anyway, PHP gave me the same error.
What is happening here?
I'm working With NetBeans for MAC
When is a space not a space?
When it's a non-breaking space!
The intention is:
" || "
207C7C20 (hex)
But what is actually in the source file is almost certainly:
" || "
207C7CA0 (hex)
(on stack overflow it won't be but I bet it is in the source file).
With a mac the problem is (using my own keyboard layout, but I am assuming it's similar in your case):
"|" = alt + 1
" " = alt + space (accidental)
So typing away, with the sequence " || " it's very easy for the alt key to still be depressed when the space bar is pressed and: voilà you get unexpected "wat" syntax errors which at face value make no sense - until you realize what the problem is.
Example:
-> cat foo.php
<?php
$foo = "x";
if (true || $foo) {
}
-> php -l foo.php
Parse error: syntax error, unexpected '$foo' (T_VARIABLE) in foo.php on line 4
Errors parsing foo.php

Categories