How do I escape quotes in URL? - php

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'";

Related

Adding functions to single quote

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 !';

How can i echo a variable from my database inside my echo?

Okay so i was wondering how i echo a variable from a database that i already have connected to a php script so basicly it would be like
echo '<img src="blablabla.com/VARIABLENAMEHERE" />'
Use double quotes, and then variables will be expanded inside the string:
echo "<img src='blablabla.com/$variable' />";
Or user string concatenation:
echo '<img src="blablabla.com/' . $variable . '" />';
you can use double quotes as the double quotes evaluates the variable inside.
echo "<img src='blablabla.com/$variablename' />";
but if you insist using single quote you can use concatenation.
echo '<img src="blablabla.com/' . $variablename . '" />';

Dynamic ID assigning

I have a problem with my page. I am displaying images using PHP Loop statement. Now I want to assign these images with different id's. Example first loop the first image displayed will have an id="img1", next loop and second image has id="img2". Numbers on the id changes based on the loop iteration variable, while "img" is constant. Here's my code:
for ($i=1;$i<=6;$i++){
echo ("<img src='gangjeong.png' width='113' id='img'.$i>");
}
but it's not working. Any help would be much appreciated.
UPDATE: I got it to work now, thanks for the answers. The working code is:
for ($i=1;$i<=6;$i++){
echo ("<img src='gangjeong.png' width='113' id='img$i'>");
}
You can't use unescaped single quotes if you start your string by single quotes.
These are the possibilities you have:
Using double quotes inside single quotes:
for ($i=0;$i<6;$i++){
echo ('<img src="gangjeong.png" width="113" id="img' . ($i+1) . '" />');
}
Using escaped single quotes inside single quotes (ugly):
for ($i=0;$i<6;$i++){
echo ('<img src=\'gangjeong.png\' width=\'113\' id=\'img' . ($i+1) . '\' />');
}
Using double quotes for starting/ending the string:
for ($i=0;$i<6;$i++){
echo ("<img src='gangjeong.png' width='113' id='img" . ($i+1) . "' />");
}
Using escaped double quotes inside double quotes (ugly):
for ($i=0;$i<6;$i++){
echo ("<img src=\"gangjeong.png\" width=\"113\" id=\"img" . ($i+1) . "\" />");
}
Quotes inside quotes are a no no without proper escaping. Also you have a missing > for the img tag. This should do:
for ($i=1;$i<=6;$i++){
echo "<img src='gangjeong.png' width='113' id='img".$i."'>";
}
Where's your escape characters? Your quotes need escaping, or put your HTML part in double quotes and PHP strings in single, or vice versa. Just make sure you're not confusing start and end quotes.
your syntax is not correct, use:
for ($i=1;$i<=6;$i++){
echo '<img src="gangjeong.png" width="113" id="img'.$i.'" alt="">';
}
Use:
for ($i=1;$i<=6;$i++){
echo "<img src=\"gangjeong.png\" width=\"113\" id=\"img$i\">";
}

passing php variable in onClick function

I want to pass the php variable value in onClick function.
When i pass the php variable, in the UI i am getting the variable itself instead I need the value in the variable.
Below is code snippet, please help me.
<?php
print '<td>';
$node = $name->item(0)->nodeValue;
$insert= "cubicle"."$node<br>";
Echo '<a href= "#" onClick= showDetails("$node");>'. $insert .'</a> ';
print '</td>';
?>
Variable parsing is only done in double quoted strings. You can use string concatenation or, what I find more readable, printf [docs]:
printf('%s ', $node, $insert);
The best way would be to not echo HTML at all, but to embed PHP in HTML:
<?php
$node = $name->item(0)->nodeValue;
$insert = "cubicle" . $node;
?>
<td>
<a href= "#" onClick="showDetails('<?php echo $node;?>');">
<?php echo $insert; ?> <br />
</a>
</td>
You have to think less about quotes and debugging your HTML is easier too.
Note: If $node is representing a number, you don't need quotations marks around the argument.
you shouldn't be wrapping $node in '"':
Echo '<a href= "#" onClick= showDetails($node);>'. $insert .'</a> ';
If you want the value of $node to be in a string, thn i would do:
Echo '<a href= "#" onClick= showDetails("' . $node. '");>'. $insert .'</a> ';
$var = "Hello World!";
echo "$var"; // echoes Hello World!
echo '$var'; // echoes $var
Don't mix up " and ', they both have importance. If you use some " in your string and don't want to use the same character as delimiter, use this trick:
echo 'I say "Hello" to ' . $name . '!';
I think you are searching for PHP function json_encode which converts PHP variable into JavaScript object.
It's more secure than passing the value right in the output.
Echo '<a href= "#" onClick= showDetails("'.$node.'");>'. $insert .'</a> ';
I have been using curly braces lately instead of concatenation. I think it looks better/is more readable, and mostly I find it is easier and less prone to human error - keeping all those quotes straight! You will also need quotes around the contents inside of onClick.
Instead of this:
Echo '<a href= "#" onClick= showDetails($node);>'. $insert .'</a> ';
Try this:
Echo '{$insert} ';
As a side note, I usually use double quotes to wrap my echo statement and strictly use single quotes within it. That is just my style though. However you do it, be sure to keep it straight. So my version would look like this:
echo "<a href='#' onClick='showDetails({$node});'>{$insert}</a>";

PHP echo does not work

I am trying to print a variable between curly braces as
Product_number{product_version}
I tried
echo "$product_number{$product_version}";
But that does not work. I don't understand why :(
try using double braces:
echo "$product_number{{$product_version}}";
You can also do:
echo "$product_number{".$product_version."}";
{ followed by $ is treated specially. It is mainly used when you want to append a string immediately at the end of a variable's value:
$v = 'hack';
echo "I {$v}ed it";
echo $product_number . "{" . $product_version . "}";
Escape the "{":
echo "$product_number\{$product_version}";

Categories