Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda