Content does not appear after entering php code

Viewed 26

I'm trying to implement a code to get an initial user name from my database, I put the code in the header section, the problem is that after I input the code the content on my page doesn't appear. Is there something wrong with my code below?

<div class="symbol symbol-light-success btn-dropdown">
     <?php
      function getProfilePicture($name)
     {
       $name_slice = explode(' ', $name);
       $name_slice = array_filter($name_slice);
       $initials = '';
       $initials .= (isset($name_slice[0][0])) ? strtoupper($name_slice[0][0]) : '';
       $initials .= (isset($name_slice[count($name_slice) - 1][0])) ? strtoupper($name_slice[count($name_slice) - 1][0]) : '';
       return '<span class="symbol-label display-5">' . $initials . '</span>';
     }
    ?>
    <?php echo getProfilePicture($_SESSION['username']);?>
2 Answers

check your php errors with add this code to first line of file

<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); ?>

and check error of code maybe is from

session_start();

You code works fine. You can tested by using:

<?php echo getProfilePicture($_SESSION['username'] ?? 'Hello World');?>
// output: HW

It seems that you dont have username in you session or is empty or you forgot to start the session: session_start();. In the last case: Add <?php session_start(); ?> on top in your script.

Related