Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing quotes in PHP

Tags:

php

mysql

I have this script that gets my info from the DB

$result = mysqli_query($con,"SELECT * FROM <table> WHERE id = 3");

while($row = mysqli_fetch_array($result))
  {
  echo '<p>'.$row['name'].'</p>' . " " .'<p>' . $row['description'] . '</p>' . " " . '<p>' . $row['price'] . '</p>';
  echo "<br>";
  echo '<img src="http://www.example.com/images/'.$row['profile_pic'].'" alt="profilepic" />';
  echo '<img src="http://www.example.com/images/'.$row['pic1'].'" alt="pic1" />';
      ^^^                                                *********
      Here is the problem because I should have sigle quote at the beginning and at the end, but I still have it above ***.And I use double quotes for the src on the <img> so those are taken too.
  echo '<img src="http://www.example.com/images/'.$row['pic2'].'" alt="pic2" />';
  echo '<img src="http://www.example.com/images/'.$row['pic3'].'" alt="pic3" />';
  }

If I do it like I intended, I would need another set of quoter( maybe triple :]] )..How can I do it the right way?

like image 424
Daniel Bejan Avatar asked Dec 06 '25 17:12

Daniel Bejan


2 Answers

The source code you have looks good. You can use \ character immediately ahead of a single or double quote to escape, like so:

$variable = "my quote's are \"like this\" ";

// output: my quote's are "like this"

Likewise:

$variable = 'this is "how my quotes" don\'t look';

// output: this is "how my quotes" don't look

like image 56
Patrick Moore Avatar answered Dec 09 '25 14:12

Patrick Moore


Rasmus Lerdorf answered you since his introduction of PHP by prefixing the variables name with $ Simply you have to do something like the following:

edited

//Place any string in double quote
echo "<p> {$row['name']} </p> <p> {$row['description']} </p> <p> {$row['price']} </p>";

The following is quote from an interview with Rasmus

$ Dollar Sign in PHP variables

When asked about why variables start with the $ sign, he explained that was meant to be able to insert variables inside literal string values, so a mark would need to be used to distinguish what is a variable from the rest of the string.

Since he wanted the variables to look the same inside and outside a string, he has chosen the $ sign to start variables, inspired in the solution that Perl also adopted.

like image 25
SaidbakR Avatar answered Dec 09 '25 14:12

SaidbakR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!