1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jokemiri
  • 33
  • 1
  • 7
  • 2
    `echo sprintf("'%s%s'", base_url(), 'index.php?admin/product/delete/1');` – Steve May 27 '16 at 14:30
  • What is this `base_url()` function? A built-in? Your own? There is a function of that name in [an answer to *How do I get the base URL with PHP?*](https://stackoverflow.com/questions/2820723/how-do-i-get-the-base-url-with-php/20075147#20075147). – Peter Mortensen Nov 29 '19 at 18:50
  • OK, the OP has left... – Peter Mortensen Nov 29 '19 at 18:50
  • @PeterMortensen regarding your recent edits, nobody formats PHP code like that. This is not XML. There are no extra indents after the opening tag and the closing tag is generally omitted unless there is some non-PHP code that follows it. See e.g. https://www.php-fig.org/psr/psr-2/ – MicE Nov 30 '19 at 23:43

4 Answers4

5

If you really want to use only single quotes, try this:

echo '\'' . base_url() . 'index.php?admin/product/delete/1\'';
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Johannes
  • 64,305
  • 18
  • 73
  • 130
4

Just simple:

echo "'" . base_url() . "index.php?admin/product/delete/1'";
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ksno
  • 487
  • 1
  • 4
  • 21
4

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}'";
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MicE
  • 5,038
  • 2
  • 29
  • 26
  • If `$baseurl` wasn't followed by an alphanumeric ("`i`"), the `{}` pair wouldn't be necessary (e.g. `echo "'$baseurl(index.php?admin/product/delete/1";` (a left parenthesis after) outputs `'http://pmortensen.eu/world/(index.php?admin/product/delete/1'`). Tested with PHP 7.1.33 (approx. 2019-10-23). – Peter Mortensen Nov 29 '19 at 19:06
  • 1
    Both `${baseurl}` (so-called [simple syntax](https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.simple)) and `{$baseurl}` (so-called [complex syntax](https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex)) works. – Peter Mortensen Nov 29 '19 at 20:13
3
echo "'" . base_url() . "index.php?admin/product/delete/1'";
gbalduzzi
  • 9,356
  • 28
  • 58