Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
why the php condition not work unless i echo the variable?

in the code below, if I echo the variable the condition will work if I don't echo the variable the condition will not work!

what is the error?

the code:

$msg=$_SESSION['$msg'];
echo $msg;
if($msg != null){ ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script >
    swal.fire({
                icon: "success",
                title: "success",
                showConfirmButton: false,
                timer: 1300
        })
 </script>

<?php } ?>

edited code: even this does not work unless echo!

$msg="ss";
if(!empty($msg)){ ?>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script >
    swal.fire({
                icon: "success",
                title: "success",
                showConfirmButton: false,
                timer: 1300
        })
 </script>

<?php } ?>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There are few problems in the code:

  1. Call session_start(); before using $_SESSION.

  2. $_SESSION['$msg'] can be undefined and can trigger notice. You should check if the key exists with isset($_SESSION['$msg']).

  3. Session key $msg is a bit strange. You don't need the $ for session keys.

  4. If you want to check for not null, use strict comparison !==.

<?php 
session_start();
$msg = isset($_SESSION['$msg']) ? $_SESSION['$msg'] : null;
echo $msg;
if($msg !== null){ ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script >
    swal.fire({
                icon: "success",
                title: "success",
                showConfirmButton: false,
                timer: 1300
        })
 </script>

<?php } ?>
about 4 years ago · Juan Pablo Isaza Relatório

0

Firstly, in line number 1 you should pull the session key, so if its a variable it should be 
$msg=$_SESSION[$msg]; 
else it should be the key name
$msg=$_SESSION['msg']; //msg can be replaced by your key name.

You can check directly whether the key exist or not and if it is empty as below: 
       if(isset($_SESSION['msg']) && $_SESSION['msg'] != ''){ ?>
        <script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
        <script >
        swal.fire({
                    icon: "success",
                    title: "success",
                    showConfirmButton: false,
                    timer: 1300
            })
       </script>
    
       <?php } ?>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda