Pass variable value inside text with echo

Asked

Viewed 2,853 times

0

I’m trying to add a value to a variable within a text with echo and I’m not getting it, the value is available because I can access it.

What I have is this, I couldn’t insert the code properly, so the image:

inserir a descrição da imagem aqui

Watching by the console of Chrome have it:

inserir a descrição da imagem aqui

print_r result($_SESSION):

Array ( [usuarioID] => 124 [nomeUsuario] => Valter Ferreira Martins [email] => [email protected] [IdCategoria] => 0 [Cliente] => 4 [produtos_1417] => 13 [produtos_1676] => 0 [produtos_1207] => 0 [produtos_1306] => 1 [produtos_1508] => 1 )

When I place the mouse pointer over the Add variable button it is no value, but in the console the value is available.

  • 1

    Put the source code there, use the button { } to display it correctly. Give a print_r($_SESSION); tbm

  • In the href has double quotes remaining, remove one of them

3 answers

2


The link in the image seems mounted wrong, has a double quote left, remove it

Change:

<a href="produtos.php?Cliente="'.$_SESSION['Cliente'] .'" class="btn ....
aspa a mais ------------------^

To:

<a href="produtos.php?Cliente='.$_SESSION['Cliente'] .'" class="btn ....
  • Thanks @rray, solved the problem, I was not able to see those quotes remaining.

1

To use echo in php Voce you need to use double quotes or concatenate, the php code always stays inside <?php ... ?> in html.

<?php
   $variavel = 123;

   //aspas duplas permite usar variaveis sem concatenar
   echo "<p>o numero é $variavel</p>";

   // ou 
   echo '<p>o numero é '.$variavel.'</p>';

?>
  • Thanks @Ivo Ribeiro, helped a lot.

1

Variables can only receive the value inside double quotes. Single quotes is for literals in PHP.

Right:

echo "$minhaVariavel";

Wrong:

echo '$minhaVariavel';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.