in C# you can declare a literal string that you don't expect to have any escaping in it like:
string myString = #"This\is\some\string\";
For PHP is there anyway to declare a string literal in this way without needing to escape every slash? I know someone out there will ask me "what have you tried?" so for the sake of completeness I will list what doesn't work:
$myString = "This\is\some\string\";
$myString = 'This\is\some\string\';
Use single quotes and you should be good (less single quotes of course). If you really don't want to have to escape anything try nowdoc.
EDIT: assuming PHP >=5.3 (thank you Sammitch)
as sequoia mcdowell pointed out heredocs/newdocs are the answer. like so:
$myString = <<<'EOT'
This\is\some\string\
EOT;
Related
eval("\$data = $myvar('https://www.example.com/json/id='". $_GET['name'] ."'))';
This got me an error, how to concatenate it properly?
Eval is dangerous, but for your specific question above, the quotes were off at the end, and myvar is supposed to be a function. See below:
eval('$data = myvar("https://www.example.com/json/id='. $_GET['name'] .'");');
If you use double quotes like "$data" then $data will first be evaluated and the result or value will be eval'd instead. This is one of the risks of using eval(). If you use double quotes, then escape the $ signs like so:
eval("\$data = myvar('https://www.example.com/json/id=". $_GET['name'] ."');");
Demo: IDEOne
eval("\$data = $myvar('https://www.example.com/json/id=". $_GET['name']."');");
i have a little problem with eval.
here is my code:
$postbit = $template->output('posts_list_index'); // output string: $postslist it's ok
eval('echo $postbit;'); // output string: $postslist (?)
Thanks :)
I think in eval() function you can able to replace string with values. Please refer http://in3.php.net/eval.
and the solution for your's is:
eval("$postbit = ".$template->output('posts_list_index'."); ");
echo $postbit ;
try this one.. I am not sure about it.
Javadewd's Answer is correct. I prefer using double quotes instead of single quotes. PHP Manual states
Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
Eval requires that you escape your $:
eval('echo \$postbit;');
I'm encoding it like so..
json_encode($array_list, JSON_UNESCAPED_SLASHES)
Ex: \n turns into \\n, \r\n turns into \\r\\n
But, it's still escaping the slashes! What's wrong and how to fix it? Thanks.
I think it is because of single and double quotes, see the examples
$arr = array("\n\r");
echo json_encode($arr,JSON_UNESCAPED_SLASHES); // ["\n\r"]
$arr = array('\n\r');
echo json_encode($arr,JSON_UNESCAPED_SLASHES); //["\\n\\r"]
working example http://codepad.viper-7.com/LvWMhq
If it is a concern when doing any MySQL queries then you can use it like this:
mysql_real_escape_string(json_encode($array))
No need to escape anything in the $array itself before this point, just let mysql_real_escape_string escape the json_encoded string.
I try to use single quotes as much as possible and I've noticed that I can't use \n in single quotes. I know I can just enter a newline literally by pressing return, but that screws up the indentation of my code.
Is there some ASCII character or something that I can type that will produce newline when I'm using single quotes?
No, because single-quotes even inhibit hex code replacement.
echo 'Hello, world!' . "\xA";
echo 'hollow world' . PHP_EOL;
Use the constant PHP_EOL then it is OS independent too.
If you are echoing to a browser, you can use <br/> with your statement:
echo 'Will print a newline<br/>';
echo 'But this wont!';
FYI it is possible to get newlines into strings without double quotes:
printf('Please%1$sgive%1$sme%1$snewlines%1$s', PHP_EOL);
Which may be useful If your irrational fear of double quotes knows no bounds. Though I fear this cure may be worse than the disease.
I wonder why no one added the alternative of using the function chr():
echo 'Hello World!' . chr(10);
or, more efficient if you're going to repeat it a million times:
define('C_NewLine', chr(10));
...
echo 'Hello World!' . C_NewLine;
This avoids the silly-looking notation of concatenating a single- and double-quoted string.
The only escape sequence you can use in single quotes is for the single quote itself.
$foo = 'That\'s great';
The only way you could insert a new line into a string created with single quotes is to insert a literal newline
$bar = 'That\'s
cheating';
There IS a difference on using single VS double quotes in PHP
e.g:
1. echo '$var\n';
2. echo "$var\n";
in 1, PHP will print literally: $var\n
in 2, PHP will have to search the location in memory for $var, and return the value in that location, also, it will have to parse the \n as a new line character and print that result
We're in the range of millionths of a second, but there IS a difference in performance. I would recommend you to use single quotes whenever possible, even knowing you won't be able to perceive this performance increase. But I'm a paranoid developer when it comes to performance.
You may want to consider using <<<
e.g.
<<<VARIABLE
this is some
random text
that I'm typing
here and I will end it with the
same word I started it with
VARIABLE
More info at: http://php.net/manual/en/language.types.string.php
Btw - Some Coding environments don't know how to handle the above syntax.
You can use this:
echo 'Hello World' . "\n";
This worked well for me:
print_r('Hello world'.PHP_EOL);
No, according to documentation, PHP recognize no special symbol in single quotes. And there is no single reason to use single quotes as much as possible
in case you have a variable :
$your_var = 'declare your var';
echo 'i want to show my var here'.$your_var.'<br>';
I changed the single quotes to doubles quotes after I faced th following problem:
$lang = array(
'let's do something...'
);
Now I have this problem:
$lang = array(
"tagline_h2" => "A paragraph...this is a link"
);
What should I do?
As you are having double-quotes in a double-quoted string, you have to escape the double-quotes inside the string, using a backslash :
"A paragraph...this is a link"
See Double quoted string in the manual : it states that PHP will interpret the \" sequence as a double-quote (and \n will interpreted as a newline, and there are a couple of other of those sequences)
I like to use urlencode() and urldecode() to store strings temporarily. That way I don't have to think about double-quotes, quotes, ampersands, newlines or tabs or in fact anything.
Two caveats:
The academics will point out that it performs worse, and I would respond by saying that more time is wasted thinking about it than dealing with it, since it's a PHP website and not a particle accelerator. (Or is it?)
You have to urldecode() it when you're going to show it.
PLEASE NOTE that for the problem you describe, urlencode()/urldecode() would be overkill. I only mentioned this for future reference, but in your particular case, the escaping is far more appropriate as described in the accepted solution.
Even better, if you know that you are going to be dealing with quotes, why not use the heredoc and nowdoc syntaxes?
That way, you can do:
$lang = array(
"tagline_h2" => <<<LINK A paragraph...this is a link
LINK
);