Let's say this is my script down here, as you can see I've used multiple " and '. These quotations conflict in ending the current php variable, so it basically sees this:
$message = "<?php echo '<div class="
As a string, whilst the quotation is only to define the class, not to end the variable. I've tried using ' but then it conflicts with the echo, so I'm kinda stuck at the moment.
<?php
$message = "
<?php
echo '<div class="gebruiker">';
$fh = fopen('_gebruiker.txt','r');
while ($line = fgets($fh)) {
echo($line);
}
fclose($fh);
echo '</div>';
?>
";
**MORE PHP CODE HERE**
?>
How can I use multiple quotations in one PHP script without them having conflicts.
If you use single quotes outside, you need to escape all single quotes inside, but can use double quotes and the dollar char without escaping.
If you use double quotes outside, you need to escape all double quotes and dollar chars inside, but can use single quotes without escaping.
If you use a heredoc string, you need to escape dollar chars but can use both single and double quotes without escaping.
If you use a nowdoc string, you do not need to escape anything unless you have FOO; in the string at the beginning of a new line.
So the solution is to use a nowdoc string:
$message = <<<'EOF'
your stuff with " or ' or $ here!
EOF;
Adding a backslash so PHP can recognize just only double quotes or quotes when escaped.
Example:
echo "<div class=\"gebruiker\">";
I suggest using either Heredoc or Nowdoc
Example
$foo = 'test';
// Heredoc
$here = <<<HERE
I'm here, $foo!
HERE;
// Nowdoc
$now = <<<'NOW'
I'm now, $foo!
NOW;
Heredoc will print the contents of $foo when echoed while Nowdoc will simply echo $foo.
In the references I added below you can do more reading up on this subject.
References:
php.net - strings
stackoverflow - advantages of heredoc vs nowdoc
You can escape the quotes with a slash character like so: \"
This should work, you can't open Php tag without closing before.
<?php
$message = '<div class="gebruiker">';
$fh = fopen('_gebruiker.txt','r');
while ($line = fgets($fh)) {
$message .=$line.'<br/>';
}
fclose($fh);
$message.='</div>';
echo $message;
**MORE PHP CODE HERE**
?>
Related
I am using heredoc to create a php file which needs to use heredoc in the generated file as the file will create other files.
The <<<EOD bit works fine and I can see all the variables properly escaped.
The problem I'm having is the nested heredoc <<<EOL where \$languageStrings isn't escaped
If I use \$languageStrings and run the generated file ddd.php, the output is this:
<?php
= Array (
'LBL_LOCATION_INFORMATION' => 'Basic Information',
'LBL_CUSTOM_INFORMATION' => 'Custom Information',
'SINGLE_' => ''
);
if i use \\$languageStrings trying to esacpe the backslash i get:
<?php
\ = Array (
'LBL_LOCATION_INFORMATION' => 'Basic Information',
'LBL_CUSTOM_INFORMATION' => 'Custom Information',
'SINGLE_' => ''
);
How can I correct this so i get $languageStrings = Array (? I did think about trying to open the file and insert into a specific line but the file will always be different.
I'm open to suggestions
Here is an example of my heredoc
$text = <<<EOD
This is some text
This is some more text
This is a \$variable //outputs this is a $variable
This is some more text
EOD;
$text .= <<<EOD
\n
\$targetpath = 'modules/' . \$moduleInstance->name;
if (!is_file(\$targetpath)) {
mkdir(\$targetpath);
mkdir(\$targetpath . '/language');
// create the language file
\$languagepath = 'languages/en_us';
\$languageContents = <<<EOL
<?php
\$languageStrings = Array (
'LBL_LOCATION_INFORMATION' => 'Basic Information',
'LBL_CUSTOM_INFORMATION' => 'Custom Information',
'SINGLE_\$moduleInstance->name' => '\$moduleInstance->name'
);
EOL;
file_put_contents(\$languagepath . '/' . \$module->name . '.php', \$languageContents);
}
EOD;
file_put_contents('ddd.php', $text);
I fixed it by creating a variable outside of the heredoc
$strings = '\$languageStrings';
and replacing \$languageStrings with $strings = Array ( in the heredoc string
The output is what I expected
Need a single quotes like Heredoc. This name is Nowdoc.
Nowdocs are to single-quoted strings what heredocs are to double-quoted strings.
$foo = "bar";
echo <<<STR
str $foo
STR;
echo <<<'STR'
str $foo
STR;
OR
echo "str $foo";
echo 'str $foo';
Expected Result:
->str bar
->str $foo
Since PHP 5.3.0, the opening Heredoc identifier may optionally be double quotes
<<<"FOO"
FOO;
same as
<<<FOO
FOO;
I prefer use every time double-quoted heredoc for intelligibility, although it is optional.
Doc says: php.net
Nowdocs are to single-quoted strings what heredocs are to
double-quoted strings. A nowdoc is specified similarly to a heredoc,
but no parsing is done inside a nowdoc. The construct is ideal for
embedding PHP code or other large blocks of text without the need for
escaping. It shares some features in common with the SGML construct, in that it declares a block of text which is not for
parsing.
A nowdoc is identified with the same <<< sequence used for heredocs,
but the identifier which follows is enclosed in single quotes, e.g.
<<<'EOT'. All the rules for heredoc identifiers also apply to nowdoc
identifiers, especially those regarding the appearance of the closing
identifier.
i'm using php on_start and ob_get_contents to echo html and store in a variable. However when I json encode and check the output it doesn't output the entire string. Could anyone help point out what I'm doing wrong
ob_start();
echo'<img src=\"images/editphotohover.png\"/>\"';
$photo = ob_get_contents();
ob_end_clean();
I get only get the ending anchor tag
in the json encode output
There is no need to escape double quotes here
echo'<a href=\"javascri...
just write this:
echo'<a href="javascri...
Double quotes are kept while in single quotes!
Additionally, note that escaping within single quotes has no effect:
"\t" renders as a TABULATOR character
'\t' renders as \t
The PHP documentation states this:
To specify a literal single quote, escape it with a backslash (\).
To specify a literal backslash before a single quote, or at the end of the string, double it (\\).
Note that attempting to escape any other character will print the backslash too.
Therefore, how about this code:
echo'<a href="javascript:pixlr.edit(
{ image: \'http://mywebite.com/uploads/$photo\',
title: \'' . $photoFileNameProper . '\',
service: \'express\',
exit:\'http://mywebsite.com/home\',
method: \'get\',
locktarget: \'true\',
target: \'http://mywebsite.com/plixr.php\',
locktitle: \'true\'
});"
id = "uploadedPhoto"
title = "click to enhance photo">
<img src="images/editphotohover.png"/>
</a>'
;
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 8 years ago.
in HTML
we can
face='Tahoma'
face="Tahoma"
and in PHP
$name = "Junaid";
$name = 'Junaid';
$names = array('Junaid','Junaid','Junaid');
$names = array("Junaid","Junaid","Junaid");
now all these statements are correct with single or double quotes but
what difference does it make
which is the preferred method
what types of quotes to use where
which one of the following is correct
$link = "www.anyLINK.com"
echo "<a href=' ". $link ." '>Click Here</a>"
echo "<a href= ". $link ." >Click Here</a>"
The difference between single and double quotes in PHP is that PHP will read variables inside of double quotes but not single. For example:
<?php
$variable = "test";
echo "Can you see this $variable"; // Can you see this test
echo 'Can you see this $variable'; // Can you see this $variable
?>
The single quote will be read literal, where was double will attempt to replace the $variable with it's value.
Optimization Differences
As pointed out in the comments below, single quotes tend to be faster than double. In a quick benchmark, double quotes with any $'s escaped is the fastest vs single and double with and without $variables in the string. See http://codexon.codepad.org/54L3miwN
See http://php.net/manual/en/language.types.string.php.
In particular, variables are expanded in double quotes:
$foo = 42;
print('foo is $foo'); // foo is $foo
print("foo is $foo"); // foo is 42
In HTML, it doesn't matter at all.
In PHP, it does. Using the single-quotes prevents variables from being interpreted; for instance, echo '$foo'; will print "$foo". Not the variable, just the characters. Also, you have to escape single-quotes within single-quotes, but not single-quotes within double-quotes, etc. I answered this question before here.
As for your second question, they're both wrong. It should be:
echo "<a href='". $link ."'>Click Here</a>"
or, better yet:
echo "<a href='$link'>Click Here</a>"
or, better still, a templating engine like Smarty TPL.
In html source
\xE6\x82\xA0
result is "\xE6\x82\xA0"
but in php
<?php echo "\xE6\x82\xA0"; ?>
result is "悠" (character for \xE6\x82\xA0 )
what can be done to make php echo \xE6\x82\xA0?
If you want to print the actual string \xE6\x82\xA0, simply replace the double quotes with single quotes. Strings in single quotes are not parsed for escape sequences.
<?php echo '\xE6\x82\xA0'; ?>
Try escaping your slashes.
<?php echo "\\xE6\\x82\\xA0"; ?>
or simply use single quotes instead of double quotes
<?php echo '\xE6\x82\xA0'; ?>
or you can simply output directly
?>\xE6\x82\xA0<?php
Escape the slash characters.
<?php echo "\\xE6\\x82\\xA0"; ?>
Or write the string directly in template mode.
?>\xE6\x82\xA0<?php
simple problem baffling me...
i have a function:
function spitHTML() {
$html = '
<div>This is my title</div>\n
<div>This is a second div</div>';
return $html
}
echo $spitHTML();
Why is this actually spitting out the \n's?
Backslashes used in single quote strings do not work as escape characters (besides for the single quote itself).
$string1 = "\n"; // this is a newline
$string2 = '\n'; // this is a backslash followed by the letter n
$string3 = '\''; // this is a single quote
$string3 = "\""; // this is a double quote
So why use single quotes at all? The answer is simple: If you want to print, for example, HTML code, in which naturally there are a lot of double quotes, wrapping the string in single quotes is much more readable:
$html = '<div class="heading" style="align: center" id="content">';
This is far better than
$html = "<div class=\"heading\" style=\"align: center\" id=\"content\">";
Besides that, since PHP doesn't have to parse the single quote strings for variables and/or escaped characters, it processes these strings a bit faster.
Personally, I always use single quotes and attach newline characters from double quotes. This then looks like
$text = 'This is a standard text with non-processed $vars followed by a newline' . "\n";
But that's just a matter of taste :o)
Because you're using single quotes - change to double quotes and it will behave as you expect.
See the documentation for Single quoted strings.
Change ' to " :) (After that, all special chars and variable be noticed)
$html = "
<div>This is my title</div>\n
<div>This is a second div</div>";