How can i echo " i mean if i want to show: "Patrik", i would do:
<?php echo " "Patrik" "; ?>
but as you know you cant do this
Escape double quotes with \ when full string itself is in double quotes:
<?php echo " \"Patrik\" "; ?>
Or:
<?php echo ' "Patrik" '; ?>
More Info:
PHP: Double quotes vs Single quotes
http://pt.php.net/manual/en/language.types.string.php
<?php echo '"Patrik"'; ?> or.. <?php echo "\"Patrik\""; ?>
<?
echo " \"Patrik\" ";
// or
echo '"Patrick"';
?>
if it's HTML or XML, you can use " as substitute for the double-quote.
Related
I want to insert a URL all into single quotes. I have tried using both single and double quotes, but it is not working. I checked this question - Escaping single quote in URL link, but it did not help:
<?php
echo '\'' base_url().'\'index.php?admin/product/delete/1\'';
echo '\''; echo base_url().index.php?admin/product/delete/1\'';
echo '\'. base_url().index.php?admin/product/delete/1\'';
echo "\'"; echo base_url().'index.php?admin/product/delete/1\'";
?>
I am trying to achieve this:
'http://localhost/my_site/index.php?admin/product/delete/1'
The URL in single quotes.
If you really want to use only single quotes, try this:
echo '\'' . base_url() . 'index.php?admin/product/delete/1\'';
Just simple:
echo "'" . base_url() . "index.php?admin/product/delete/1'";
You can use double-quotes to pass variables into strings in PHP. On top of enhancing readability, this also saves you the trouble of escaping single quotes.
So for example:
<?php
$foo = 'bar';
echo 'single-quoted string: $foo <br />';
echo "double-quoted string: $foo <br />"; // "$foo" works, "{$foo}" is better
echo "double-quoted string: \$foo <br />"; // Backslash escapes
?>
Prints (emphasis mine):
single-quoted string: $foo
double-quoted string: bar
double-quoted string: $foo
So to update your example:
<?php
/* ... */
$baseurl = base_url();
echo "'{$baseurl}index.php?admin/product/delete/1'";
?>
Or how about we put it all into a variable and gain a little readability:
<?php
/* ... */
$baseurl = base_url();
$fullurl = $baseurl . 'index.php?admin/product/delete/1';
echo "'{$fullurl}'";
?>
echo "'" . base_url() . "index.php?admin/product/delete/1'";
Help me
How can I insert a function inside a single quote?
<php
echo ' Hello world <?php function(); ?> ';
what's the correct syntax ?
If you have to use single quotes then you would concatenate (.) the function:
echo ' Hello world ' . function();
You have to cancatenate it.
echo 'Hello '. yourFunction() . 'world !';
<?php while($result = mysql_fetch_array( $resulta )) {
echo "<tr>";
echo "\t\t<td>{$result['denumire_locatie']}</td>\n";
echo "\t\t<td>{$result['tip_locatie']}</td>\n";
echo "\t\t<td>{$result['judet']}</td>\n";
echo "\t\t<td>{$result['localitate']}</td>\n";
echo "\t\t<td>{$result['strada']}</td>\n";
echo "\t\t<td>{$result['numar']}</td>\n";
echo "\t\t<td>{$result['telefon']}</td>\n";
echo "\t\t<td>{$result['fax']}</td>\n";
echo "\t\t<td>{$result['email']}</td>\n";
echo "\t\t<td>{$result['descriere']}</td>\n";
echo "\t\t<td>click pentru detalii</td>\n";
echo "</tr>";
}
When I run I get the following error message:
Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\SITE\index.php on line 157
Line 157 is the following one:
echo "\t\t<td>click pentru detalii</td>\n";
Can anyone see the problem?
You are trying to use double quotes wrapped by double quotes...when you get this situation either alternate single and double quotes or escape the inner quotes with \ like \".
But escaping is the better choice in IMHO, when you want to maintain
consistency while writing the attributes of html tags.
Either:
echo "\t\t<td>click pentru detalii</td>\n";
Or
echo "\t\t<td><a href='/oferta.php?id={$result['id_oferta']}'>click pentru detalii</a></td>\n";
Changed code:
<?php while($result = mysql_fetch_array( $resulta ))
{
echo "<tr>";
echo "\t\t<td>{$result['denumire_locatie']}</td>\n";
echo "\t\t<td>{$result['tip_locatie']}</td>\n";
echo "\t\t<td>{$result['judet']}</td>\n";
echo "\t\t<td>{$result['localitate']}</td>\n";
echo "\t\t<td>{$result['strada']}</td>\n";
echo "\t\t<td>{$result['numar']}</td>\n";
echo "\t\t<td>{$result['telefon']}</td>\n";
echo "\t\t<td>{$result['fax']}</td>\n";
echo "\t\t<td>{$result['email']}</td>\n";
echo "\t\t<td>{$result['descriere']}</td>\n";
echo "\t\t<td>click pentru detalii</td>\n";
echo "</tr>";
}
echo "</table>";
In this line the double quotes end your echo string.
echo "\t\t<td>click pentru detalii</td>\n";
^
To avoid (escape) this behavior put a backslash infront of it
echo "\t\t<td>click pentru detalii</td>\n";
^ ^
echo "\t\t<td><a href="
^ end of string
Replace with:
echo "\t\t<td>click pentru detalii</td>\n";
You have to escape the " as you use it as delimiter.
Try this:
echo "\t\t<td>click pentru detalii</td>\n";
You have to escape your " so PHP won't get confused... .
how can i make sure these quotation marks become valid in PHP?
<?
echo "oaktree.addItem('test1<img src='img.png'>', branch1, '');";
echo "oaktree.addItem('test2<img src='img.png'>', branch1, '');";
?>
the problem is in the tag...
thanks
Your original code is correct as far as PHP syntax is concerned, but it does not output correctly formatted JavaScript, as you are already aware. You can use double quotes inside double quotes in PHP as long as you escape them properly. You can do
<?
echo "oaktree.addItem('test1<img src=\"img.png\">', branch1, '');";
echo "oaktree.addItem('test2<img src=\"img.png\">', branch1, '');";
?>
Try this:
<?php
echo <<<EOT
oaktree.addItem('test1<img src="img.png">', branch1, '');
oaktree.addItem('test2<img src="img.png">', branch1, '');
EOT;
?>
I was wondering how can I print out whitespace in the following code below.
<?php echo "$first_name", "$last_name" ; ?>
Just, well, add whitespace to your output?
<?php echo "$first_name $last_name with some whitespace" ; ?>
Sometimes, it the obvious answer is right - even in PHP!
If you want to concatenate several strings without the double quotes (e.g. when using single quotes):
$string = $first_name . ' ' . $last_name; // The dot is a concatenation operator
<?php
echo "$first_name $last_name";
?>
Notice that when you use ' any var aren't display, with " does.
So, good is:
<?php echo "$first_name $last_name" ; ?>
bad
<?php echo '$first_name $last_name' ; ?>
If you want to keep using commas:
<?php echo $first_name, ' ', $last_name; ?>