Unexplained syntax error with logical OR ( || ) - php

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

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.

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);
?>

How to close a string the way that bash does in php CLI or suppress error output?

If you create a simple php script with this code:
shell_exec('"');
And run it with:
php myscript.php
It gives the following error in bash on my Mac:
sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file
I've tried everything I can think of:
ob_start();
#shell_exec('"');
ob_end_clean();
#shell_exec('" 2> /dev/null');
But no matter what I try, I can't suppress the message. The problem is that I'm creating a unit test to stress test $argc, $argv and the getopt() command for a general purpose tool, and I need to call the script hundreds of times with various inputs containing random characters like '"=: etc.
So I need to be able to either suppress the error output, or detect imbalanced quotes and append either a ' or " to the end of the string. I found this regex to detect single and double quoted strings:
PHP: Regex to ignore escaped quotes within quotes
But I'm having trouble visualizing how to do this for a general bash command that has a mix of quoted and unquoted arguments. Is there a built-in command that would tell me if the string is acceptable without throwing an error? Then I could try appending either "'" or '"' and I'm fairly certain that one of them would close the string. I'm only concerned with preventing the error message for now, because I'm just throwing random input at the script at this point.
Thanks!
The child shell process is writing the error message to STDERR, which it inherited from the parent PHP process. You could close the parent's STDERR file handle in the PHP script before running shell_exec().
<?php
fclose (STDERR);
shell_exec('"');

Weird unexpected T_STRING error

I have a weird PHP error in a current Symfony2 project:
unexpected T_STRING in /blahblah/Foo/BarBundle/Entity/User.php on line 1
This is a pretty standard error, usually linked to a mismatched pair of " or '.
But here is the code of the file User.php
<?php
namespace Foo\BarBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
* #ORM\Entity(repositoryClass="Foo\BarBundle\Entity\UserRepository")
*/
class User extends \FOS\UserBundle\Entity\User
{
// classic user entity
The line <?php is line #1. There is no quotes, and the weird thing comes from the fact that this error only appear on my staging server: on 2 development machines with local copies of the code, it behaves as expected with no error or warning.
The file is the correct one, the cache was emptied. I thought that it might be an encoding error but it does not seem to be this. I also thought of namespace issues, but the PHP version on the server is correct (5.3.16)
Do you have any idea what this error can stem from, or in which direction I could search ? Thanks in advance.
Most coding conventions that I worked with strictly require using LF ('Unix style', '\x0A') line endings in the scripts. And whoever managed to submit code with CRLF or, god forbid, CR had to endure a royal share of pain. )
It may seem not such a big deal, yet it can save you hours of searching for a weird error - such as in this case.
I think it's an encoding problem of your file. If your project is encoded UTF8 for example, open your file with your text editor and choose the option "Encoding" -> UTF-8 without BOM.

Mess in php file when edited on different computers?

I work home with Dreamweaver cs5 on windows 7 and on work on windows xp Dreamweaver cs5.5
When i edit file home and open it from ftp on my work all spaces between lines of code double when i get home and open file there are additional space line added to each empty line why is this happening?
e.g.
if (true) {
test();
};
now looks like
if (true) {
test();
};
and this is everywhere.
Also sometimes rarely i open file and my php code does not have no new lines its all on the same big line like this.
if (true) { test(); };
Someone knows whats the problem here?
Maybe it will be not best answer, but...
Dreamweaver is wrong tool, use normal IDEs - PhpStorm or NetBeans.
Open the file
Click CTRL + F
Select "Current document" in "Find in" (You can also select the
folder if you have multiple files)
Search in "Source code"
Tick "Use regular expression"
Type "[\r\n]{2,}" (without quotes) in "Find"
Type "\n" (without quotes) in "Replace"
Press "Replace All"
This is something that does come up with Dreamweaver and it happens when you're on a Windows machine and you download a file from a Unix server.
There's a fairly easy fix to immediately get rid of the extra line breaks at least, if that's the current issue, and to prevent them in the future:
http://www.jaredstenquist.com/2009/02/13/removing-extra-linebreaks-and-spaces-in-dreamweaver/
When removing extra line breaks, it will remove all empty breaks so be sure this is what you want - you may have to create some manual breakss over again.
As for line breaks not appearing in rare cases, I suspect setting line break type to match your sever's as suggested in that article may address that issue as well.

Categories