-3

My source code here

<?php   
$a = "$b = dfsdf ";

echo $a; 

?>

I want to get out put result as below

$b = dfsdf
  • Not sure if PHP can do it. It's security risk. PHP can handle with dynamic variables, may be that's what you need. https://www.dummies.com/programming/php/how-to-use-php-variable-variables/ – DuhVir Sep 29 '19 at 08:52
  • how to add social character in the variable. Like $ and = Sign in the string. – Naeem Hussain Sep 29 '19 at 08:53
  • I suppose that you want do it. But I do not know such method and fast google search shows nothing. Logic says that PHP can't do it. It's very big security risk. – DuhVir Sep 29 '19 at 08:54
  • Or you can check `eval()` function. Not sure that it's enough – DuhVir Sep 29 '19 at 08:55
  • @DuhVir I read the question as the OP is having difficulty echoing out the assignment. I don't think they wish to evaluate the code. – Progrock Sep 29 '19 at 11:20
  • @Progrock Maybe, it actually not clear. Let him choose what he want ) – DuhVir Sep 29 '19 at 11:32
  • 1
    @DuhVir or ask the OP to clarify. – Progrock Sep 29 '19 at 11:35
  • @Progrock yes, noted. When I were ansering I were sure that understood correctly – DuhVir Sep 29 '19 at 11:38

5 Answers5

0

use single quote instead of double quotes i.e. $a = '$b = dfsdf';

OR

$a = "$\b = dfsdf";
echo stripslashes($a); 

https://3v4l.org/BHDSP

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

Read about the difference in single and double quotes. You need to use single quotes here:

$a = '$b = dfsdf ';

echo $a; 
freeek
  • 985
  • 7
  • 22
  • You can use single quotes there, sure. But you could also escape the dollar with a backslash if the string is contained within double quotes. – Progrock Sep 29 '19 at 11:21
  • Why isn't the OP's code working as they expect? – Progrock Sep 29 '19 at 11:30
  • I would still recommend using single quotes here. – freeek Sep 29 '19 at 11:55
  • Personally I'd go the nowdoc route for larger samples so as to not have to worry about escape sequences. It's likely the OP forgot to include quotes in the string assignment anyway, and if they had in their code sample it may complicate matters. It's interesting to know why you'd recommend one method over another. – Progrock Sep 29 '19 at 12:12
  • @Progrock let's wait for OP. Nowdoc has problems with testing if you format text inside. Also, there are a lot of talks about removing it. Escaping in double quotes does the job, but you waste interpreter time to check if it has something to parse inside. I provided a link for OP for now, will add more info if OP comes back with more updates. – freeek Sep 29 '19 at 12:28
  • Please elaborate on nowdoc problems, it's advertised for precisely the job in hand. – Progrock Sep 29 '19 at 12:43
  • (That's quite a yucky link, and indicitive perhaps of a failure of the Php manual/documentation.) – Progrock Sep 29 '19 at 12:51
0

See the manpage on strings:

https://www.php.net/manual/en/language.types.string.php

You can use an escape sequence for special characters when using double quotes. In your case Php is attempting variable substitution ($b is likely parsed to an empty string and will not show as $b, this should also emit a Php notice.)

Escape the dollar like so:

echo "\$foo = bar";

You can use single quotes:

echo '$foo = bar';

Ideally you want to use a Nowdoc:

From the manpage above:

no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.

echo <<<'PHP'
$foo = bar
PHP;

Output for each of these:

$foo = bar

Rather than echoing directly, you can of course assign in the same manner to a variable.

Note that $foo = bar; is a bad code sample, as the bar likely should be quoted.

Progrock
  • 7,373
  • 1
  • 19
  • 25
0

Use single quotes

$a = '$b = dfsdf ';
Abbasi
  • 1
0

This might be the solution you are looking for. Replace double qoutes with single one. As single qoutes assigns as it things given inside of it.