This question already has an answer here:
PHP $_Server Not Working Properly [duplicate]
(1 answer)
Closed 8 years ago.
$a="something";
echo ' <div id="content">$a, but not sure how to do it</div>'
How can I print $a's value inside echo?
echo " <div id=\"content\">$a, but not sure how to do it</div>";
Please read the difference between single and double quotes in PHP.
echo "<div id=\"content\">" . $a . ", but not sure how to do it </div>";
The \'s are escape sequences that allow you to have quotation marks within strings (otherwise they would terminate the string). The .'s are concatenation.
there are many ways
1) use " instead of '
echo " <div id='content'>$a, but not sure how to do it</div>"
2) use {} with " if you are want to print something complex and want your editor to format it correctly. most of the time i use it with arrays or when calling a function ex. {$arr['a']} instead of $arr['a'].
echo " <div id='content'>{$a}, but not sure how to do it</div>"
3) just do this
echo ' <div id="content">' . $a . ', but not sure how to do it</div>'
Related
This question already has answers here:
Difference between period and comma when concatenating with echo versus return?
(9 answers)
Closed 7 years ago.
In PHP how do you know if you should use each form of echo, and what is the proper uses for each:
with the period:
echo"<div>
<img src='".$row['image']."'>
".$row['text']."
</div>
with the comma:
echo"<div>
<img src='",$row['image'],"'>
",$row['text'],"
</div>
with the {}:
echo"<div>
<img src='{$row['image']}'>
{$row['text']}
</div>
preset:
$image=$row['image'];
$text=$row['text'];
echo"<div>
<img src='$image'>
$text
</div>
More specifically I'm looking for the difference in how PHP will add the text to the HTML dump on the front end.
. concatenates strings (+ in most languages, but not PHP). PHP manual operators
, separates arguments (echo can take more than 1 argument). PHP manual echo
In your first example
echo" ".$row['text']." must be changed to
echo " ".$row['text']." which is write syntax to concatenate two string.
For second example
echo" ",$row['text']," must be changed to echo " ",$row['text']," I don think this is valid syntax in php .
$image=$row['image']; $text=$row['text']; echo" $text must be like
$image=$row['image']; $text=$row['text']; echo $text;
with period:
echo"<div>
<img src='".$row['image']."'>
".$row['text']."
</div>";
src='".$row['image']."' denotes the concatenation of $row['image']
with comma:
echo"<div>
<img src='",$row['image'],"'>
",$row['text'],"
</div>";
^The comma , defines the string separation in the above example.
preset:
$image=$row['image'];
$text=$row['text'];
echo"<div>
<img src='$image'>
$text
</div>
^While this would still work with single '$image' A better practice would be '".$row['image']."' wrapped around quotes, i.e. below:
echo "<div>
<img src='".$image."'>
".$text."
</div>";
Note: Keeping it simple, . for concatenation and , for separation.
^An Example:
<?php
$str = "Hawas" . "Ka" . "Pujaari";
echo $str;
$str2 = "Hawas, " . "Ka, " . "Pujaari";
echo $str2;
?>
This question already has answers here:
PHP: Work with a large string with quotes
(3 answers)
Closed 8 years ago.
I have some html text and want to put it into a variable.
Note: I can't use ?> .... <? here, I need html in variable.
The problem is about quotes symbol — ". I want my html will be with ", not with ', so I can't use this:
$var = "some 'name'";
I can't use next code, because I want to use interpolation:
$var = 'name is "$name"'; // this code doesn't work. output is: name is "$name".
To achieve, what I want, I can use this:
$var = "some \"$name\"";
This works, but it is stupid, especially with a bit larger amount of html text, like in this:
$results['main'] .= '
<div class="col-xs-12">
<div class="pull-right">
<span class="glyphicon glyphicon-plus"></span> Добавить клиента
</div>
</div>
';
I need to escape 12 " symbols in example. It is not good at all.
You can simply break the variable string like this:
$results['main'] .= '
<div class="col-xs-12">
<div class="pull-right">
'.$name.'
</div>
</div>';
Use the PHP HEREDOC
You can declare your variables before the start of the HEREDOC and then use them in the doc without worrying about double quotes, etc. Since I do not see your vars in the html example you provided. I have made up an example.
$url = "<a href = 'http://google.com'>Google </a>";
$results['main'] .= <<<EOF
Here is a good website: $url. I hope you enjoy it
EOF;
If you want print " use entities: "e;
And all quotes in string you can replace with str_replace like this:
$myString = '';
foreach($someArrayWithStrings as $string) {
$myString .= str_replace("\"",""",$string);
}
http://www.elizabethcastro.com/html/extras/entities.html
This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
I have a textbox named 'fname'. I need to echo out the input of this box inside double quotes on a another page.
User enters: Test123
returrns: "Test123"
so how can I do that with $_POST["fname"] ?
Or you can use:
echo "Hi how are you {$_POST['fname']} ? I am fine thanks";
If you want to use it in a string surrounded by letters, simply use curly brackets {}.
Put the $_POST['fname'] to curly brackets.
Try
<?php echo('"'.htmlspecialchars ($_POST["fname"]).'"'); ?>
There are many ways:
besides the one nyarathotep mentioned:
echo sprintf('"%s"', $_POST['fname']);
printf('"%s"', $_POST['fname']);
Use this:
echo '"' . $_POST["fname"] . '"';
Or
echo "'" . $_POST["fname"] . "'";
of course if you want to you can replace ' and " with " or ' in the code...
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Difference between single quote and double quote string in php
Hi all
I am very new to PHP and I wanted to know what is the difference in use of ' ' and " " ?
Thanks
$name='RkReddy';
echo "$name hi hello"; //op-RkReddy hi hello
echo '$name hi hello';//op-$name hi hello
Double quotes evaluate the content of the string, the single quotes don't.
$var = 123;
echo 'This is the value of my var: $var'; // output: This is the value of my var: $var
echo "This is the value of my var: $var"; // output: This is the value of my var: 123
It WAS a perfomance issue too, 'cause evaluation time affects the interpreter, but nowadays with the current (minimum) hardware it's not an issue anymore.
Nothing much, but when using double quotes you can use a PHP variable inside the string, and the string will output the variable's value. When using a single quote it will output the variable's name without parsing the actual value. IE.:
$something = "A nice little variable";
echo "$something is printed out"; //output: A nice little variable is printed out
echo '$something is not printed out'; //output: $something is not printed out
Hope this helps!
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.