Home > Software engineering >  How to pass double quotations in a php variable
How to pass double quotations in a php variable

Time:12-23

I am attempting to include double quotations in a php variable.

The desired output in this example would be:

  • "John Smith"

Here is my latest, failed attempt...

<?php

if (isset($_POST['firstName']) && isset($_POST['lastName'])) {

  $username = $_POST['firstName'];
  $userpassword = $_POST['lastName'];
  $combined = '- ' . '\”' . $firstName . ' ' . $lastName . '\”';

}

?>

CodePudding user response:

Here is answer to your question https://stackoverflow.com/a/7999163/2585154

And the variable name is misspelled in your code. For your $combined var you are using $firstName and $lastName but there are no such variables.

Try this:

if (isset($_POST['firstName'], $_POST['lastName'])) {
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];

    $combined = "{$firstName} {$lastName}";

    // John Smith

    // or:

    $combined = "\"{$firstName} {$lastName}\"";

    // "John Smith"
}

P.S. Read this doc PHP: Strings

  • Related