I'm trying to remove repeated slashes and while I'm tying it, I get errors. It seems some characters have to be escaped but I can't tell which one.
<?php
$path = preg_replace('(\\){2,}', '$1', 'z:\\\aaa\\\\bbb\c\ddd\');
echo $path;
?>
This gives,
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE
Could somebody fix this?
This part:
'z:\\\aaa\\\\bbb\c\ddd\'
should be:
'z:\\\aaa\\\\bbb\c\ddd\\'
You're escaping the closing quotation mark with a backslash, so the string doesn't end.
Related
When I run this code on my editor, it returns an error.
My php version is 5.4.35
Code:
<?php
$name = <<<SQL my name is amit
SQL;
echo $name;
?>
Error:
Parse error: syntax error, unexpected '<<'(T_SL) on line 2
Can someone please explain why this error, or what my script is missing?
Thanks.
Heredoc syntax requires a newline character immediately after the identifier, before the string content:
A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.
$name = <<<SQL
my name is amit
SQL;
echo $name;
demo
I have an array where I would like to put spaces between the [] like :
$array[South Africa]=array();
But I can't... why this is not possible?
The correct way of doing this is:
$array['South Africa'] = array();
By not placing quotes around strings, PHP will first check if it is a constant, and if not assume you want to specify the string stated (and generate a warning).
This would work without a space (other than the warning and being bad practise) but with the space PHP thinks the string/constant has ended after 'South' and expects an ]. What you have specified will result in a syntax error:
unexpected T_STRING, expecting ']'
I personally would avoid using spaces for names/keys anyway but the above explains the problem you are having if you must do this.
For closing a <div>, I use onclick="this.parentNode.style.display='none';" (in HTML) and it works. But in PHP it doesn't work!
error:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/u381013597/> public_html/index.html on line 311
When I change 'none' to "none" there is no error but the close item doesn't work.
What is the problem?
When you print in PHP you have to watch out with strings. You define a string with single or double quotes. If your string contains for example single quotes and you defined the string with a single quote the quote in the string itself will mean end of string, so you have to escape them:
echo 'onclick="this.parentNode.style.display=\'none\';"';
As you see I used single quotes for printing here, and I escaped every single quote in the string itself with the backslash character: \
Otherwise, the string would ended before none (because there is a unescaped single quote that means the end of the string) and the parser would except a ; character that means end of command or a , character that marks that there will be other parameters, but you give none of them, it gets none instead. Thats why it throws that he get an unexpected T_STRING. If you look at your error message, you will see that it says the same as I did, just in a compact way:
error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/u381013597/> public_html/index.html on line 311
It also says that there is a Parse error: syntax error that means you mistyped something, and he also says where does the problem occurs.
Error messages are your friends, they give a hint about the problem. Read (or at least search for) them and you will be able to develop much faster.
When I set variables that include angle brackets (< >) or slashes I keep getting errors like the following (code simplified to focus on error):
Parse error: syntax error, unexpected '>' in D:\hosting\8499439\html\test.php on line 2
<?php
$xml = ā<Request>\nā;
?>
I also run into a lot off issues with "unexpected T_String" errors that appear to be related.
I'm running PHP5 on a GoDaddy Windows Server.
What am I doing wrong? (I get the impression I need to to do something so that special characters can be handled in my PHP).
Thanks in advance.
Your quotes are curly quotes, not straight quotes, so PHP runs into an error processing them. A string can only be recognized with straight quotes.
Use the following code:
<?php
$xml = "<Request>\n";
?>
Assuming that you have the same error elsewhere, you can probably do a simple search-and-replace to fix the error: search for one of the curly quotes, replace with a straight quote. Repeat with the other curly quote. Make sure to check for straight quotes that may need to be escaped (for instance, something like "Mary said, "I like this."" would need to be escaped as "Mary said, \"I like this.\"")
mc10 is right.
Additionally I can say, there are only ""(double) and ''(single) quotes in PHP. I suggest you to read about differences between them.
I prefer using single quotes only to keep code clear.
Trying to create a plugin for wordpress that uses jquery:
echo "$('#datepicker').datepicker({ ..... ";
The # is working as a comment i tried \# to stop it but that doesnt work. Any ideas?
Code:
$dispWidget = $dispWidget.'<script type="text/javascript">';
$dispWidget = $dispWidget.'$(function() {";
$dispWidget = $dispWidget."$('#datepicker').datepicker({";
$dispWidget = $dispWidget."changeMonth: true,";
$dispWidget = $dispWidget."changeYear: true,";
If the error is "Parse error: syntax error, unexpected T_VARIABLE" the problem is actually related to the dollar sign.
To fix this, use single quotes in your PHP strings and double quotes in your JavaScript.
echo '$("#datepicker").datepicker({ ..... ';
Single quotes are better for performance as well.
What actually happens is this:
You open a single quote, then close it (when you juts wanted to add one to the string), and then add the hash, like this:
'..stuff..'#other stuff'
What you wanted was this:
'..stuff..\'#other stuff'
That single quote has to be escaped with a backslash to be treated as a character instead of a closing quote.