issuewith single,double quotes inside string in php

2.3k Views Asked by At

I will print the string in PHP, but in a string in there middle of in string double quotes, single quotes string also .So I will not get the expected output.

My code Below:

<?php
$str = "Couldn't pay via card";
echo $str . " New "home" away from home when in Bangkok";
echo addslashes($str) . " This is safe in a database query.";
?> 

I will useaddslashes() function of PHP to display the string but there some error occurred

output :

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in on line no 5.

How it can be solved?

1

There are 1 best solutions below

6
On

You can simly use slashes before your inner quotes like:

echo $str . " New \"home\" away from home when in Bangkok";

or change to single quotes

echo $str . ' New "home" away from home when in Bangkok';

Using single quotes mixed with double quotes does no harm, you can use code like:

"This is " . 'some "sample" text! and it uses \'many\' quotes'