PHP single quotes in an echo

1

I want to show the single quotes within echo

$email = "[email protected]";
echo 'mail->addAddress('.$email.')';

It has to come out like this:

mail->addAddress('[email protected]');
    
asked by Trackless 23.05.2017 в 09:50
source

2 answers

6

Use double quotes outside and simple inside:

$email = "[email protected]";
echo "mail->addAddress('$email');";

Demo

With the single quotes, print literals. With the doubles it prints the content and its variables.

Ex:

You can also use the escape character as Diego says in his answer.

    
answered by 23.05.2017 / 09:53
source
2

You can use the escape character /:

$email = "[email protected]";
echo 'mail->addAddress(\''.$email.'\')';

Or also using double quotes on the outside:

$email = "[email protected]";
echo "mail->addAddress('$email')";

Also, in this second example, you do not need to use concatenation, since PHP searches inside the chain if there are variables to print.

    
answered by 23.05.2017 в 13:23