Php: Using a Variable Inside a Double Quotes

PHP: Using a variable inside a double quotes

$imagebaseurl = 'support/content_editor/uploads/' . $name;

or

$imagebaseurl = "support/content_editor/uploads/{$name}";

Note that if you use double quotes, you can also write the above as:

$imagebaseurl = "support/content_editor/uploads/$name";

It's good though to get in the habit of using {$...} in double quotes instead of only $..., for times where you need to insert the variable in a string where it's not obvious to PHP which part is the variable and which part is the string.

If you want the best performance, use string concatenation with single quotes.

PHP: Using a variable inside a double quotes with backshlashes

Just use string concatenation instead:

$file_name_with_full_path = 'C:\inetpub\wwwroot\upload\files\\' . $filnr . '\\' . $file;

Note you need to use \\ for the \ before the ' otherwise PHP treats it as an escaped '.

If you want to use double quotes, you just need to escape all the backslashes that occur before something that can be interpreted as a variable (or a special character e.g. \f = Formfeed):

$file_name_with_full_path = "C:\inetpub\wwwroot\upload\\files\\$filnr\\$file";

Demo on 3v4l.org

How to insert php variable inside a double quoted php string

You missed out the closing '. Also, you need to concat them using the PHP concatenation operator . to join the string with the variable:

$a['id'] = 1;
$html .= '<a href="' . $a['id'] . '">Link</a>';

How can I add single quotes to a variable in PHP

You can do

echo "\"$animal\"";

Or you can just use single quotes instead as the following:

echo '"$animal"';

Whichever you prefer.

PHP: how to add double quote to a PHP variable

Try this concatenation

echo '"' . $time . '"';

or use sprintf() like so

echo sprintf('"%s"', $time);

How to insert a double quotes in a variable in php

Just add single quotes to your string which will do what you need:

$sql = "SELECT ID FROM loto_users WHERE email='".$email."' LIMIT 1";

But, if you really want double quotes, just escape them:

$sql = "SELECT ID FROM loto_users WHERE email=\"".$email."\" LIMIT 1";

Or, alternatively, don't concatenation:

$sql = "SELECT ID FROM loto_users WHERE email='$email' LIMIT 1";
$sql = "SELECT ID FROM loto_users WHERE email='{$email}' LIMIT 1";

Is there any way to use function in PHP double quote string?

No, sorry, there's nothing like that built into PHP.

The Strings documentation describes the kinds of substitutions that are done inside double-quoted strings and heredocs.

When a string is specified in double quotes or with heredoc, variables are parsed within it.

There are two types of syntax: a simple one and a complex one. The simple syntax is the most common and convenient. It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort.

The complex syntax can be recognised by the curly braces surrounding the expression.
...

Complex syntax

Any scalar variable, array element or object property with a string representation can be included via this syntax.

Neither the simple nor complex syntax can be used to include a function call.



Related Topics



Leave a reply



Submit