Concatenate string in php

0

I want to concatenate a session variable that before its value has a 'and at the end of its value has another', as I do. I leave code

$colorHeader = $_SESSION['_colorHeader'];

For example:

The variable $_SESSION['_colorHeader'] contains #FFFFFF , and I want the variable $colorHeader equals '#FFFFFF' , there is a single quote at the beginning and one at the end.

    
asked by Francisco Javier Moivas Rodrig 29.03.2017 в 22:43
source

3 answers

2

If you're looking to concatenate the single quotes with your color, you can have some alternatives, check the official PHP documentation regarding the use of character strings .

Basically you could use the Double Quote to make your concatenation for example I'll give you the following example:

$ses['color'] = "#FFFFFF"; //entrada de color sin comillas
echo "'$ses[color]'"; //concatenación usando entrecomillado doble sin usar concatenación con punto(.)

In your case you could do the following:

$colorHeader = "'$_SESSION[_colorHeader]'";

You can also use simple quotation marks as mentioned in the response of @Giovanni, another alternative is to use Heredoc, but in this case it would be unnecessary.

    
answered by 29.03.2017 в 23:36
1

To what I understand $ colorHeader, you need it as a string variable, so it is best to initialize it as $colorHeader = ''; , then assign the value to the session variable, automatically php use the string type.

In case this is not what you need and you need to literally concatenate, do the following:

$colorHeader = "'". $_SESSION['_colorHeader'] ."'";

I hope you serve

    
answered by 29.03.2017 в 22:47
0

The single quote does not need an escape bar, so it should help you

$colorHeader = "'". $_SESSION['_colorHeader'] ."'";
    
answered by 29.03.2017 в 23:44