Send double and single quotes within a variable

2

I need to put in a variable content that has double quotes, single quotes, /, ... several characters, is there a way to put all the code within a single variable? there is not something like this $ variable < < < "" '' / \ > > > > ; I mean you can put some different character in the beginning and close with that in the end where I can include all the characters I need. That is, convert all the code into a single string. Thanks.

    
asked by Stivents 15.11.2016 в 22:55
source

3 answers

6

You need to use Heredoc, which can ignore all weirdness by removing variables and functions, according to what you interpret of what you ask, you can add html if you want, example:

 $str = <<<EOF
    "hi world!", 'bye world!';
EOF;

    echo $str;

Print:

"hi world!", 'bye world!';

Documentation

    
answered by 16.11.2016 / 13:29
source
9

You can use single quotes or double quotes to make a string:

$hola = 'hola';
$hola = "hola";

And in both cases if you print the variable you will see the word hola .

The problem is when you try to use single or double quotes within this type of strings. To do this, you will have to use the backslash \ just before the quote type, just like the type of quotes you used to open and close the variable.

Examples:

$cadena = "esto' es una \" prueba '\r";
$cadena2 = 'esto" es una \' prueba "';
echo $cadena; //Devolverá --> esto' es una " prueba '
echo $cadena2; //Devolverá --> esto" es una ' prueba "

You also have to keep in mind that when you use simple quotes to open and close a String the String will be considered as it is, that is, if for example you put \r within the variable cadena2 of the example, you will not make a line break but you will print \r within the String.

On the other hand, with the double quotes the content of the String will be evaluated. As shown in the example I added before, \r performs a line break.

The only thing that single quotes evaluate is whether you use a backslash to "escape" a single quote or a backslash ( \' or \ ).

    
answered by 15.11.2016 в 23:03
0

When I do this I ignore my double quotes

$idgestion = $_REQUEST["id_gestion"];
$idmateria = $_REQUEST["id_materia"];

$iddia = $_REQUEST["id_dia"];
$idhorario = $_REQUEST["id_horario"];
$idsemestre = $_REQUEST["id_semestre"];
$idcarrera = $_REQUEST["id_carrera"];
$nuevo1 =  "\" $idsemestre \"";
$nuevo2 = "\"$idcarrera\"";
    
answered by 09.10.2018 в 03:54