0

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?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

1 Answers1

3

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'
NoOorZ24
  • 2,914
  • 1
  • 14
  • 33
  • Its a Thumb rule for all languages, To print double quote or some special character '\' is used! – Jayesh Dhandha May 18 '18 at 05:02
  • nice but record from get from database then how ?/ – Parasmal Prajapati May 18 '18 at 05:03
  • @parasprajapati if you get record from database it will be stored in variable, you just have to add that variable like: 'some ' . $variable .' here!' – NoOorZ24 May 18 '18 at 05:05
  • $positive = $_REQUEST['positivereview']; in that it's value ="New "home" away from home when in Bangkok" then how to store it?? @NoOorZ24 – Parasmal Prajapati May 18 '18 at 05:23
  • If string is already in variable you don't have to worry about quotes, PHP has already parsed them as piece of text. Just use it as any other variable like in my previous comment. – NoOorZ24 May 18 '18 at 05:26
  • @parasprajapati only reason you are having Error in your code is because in " New "home" away from home when in Bangkok"; (home) is not interpreted as text, but something between two texts. addslashes($_REQUEST['positivereview']) wouldn't generate error if request contains string – NoOorZ24 May 18 '18 at 05:34