Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PT-83's avatar
Level 7

Php returning empty array?

Following along in the php series there's homework about arrays.

When I run the code (that's replicated from the video) it's outputting an empty array, not sure why that's happening? Any idea what im doing wrong?

index.php

<?php


$person = [
    'age' => 31,
    'height' => 5,
    'profession' => 'unkown'
];

require 'index.view.php'; 

index.view.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <ul>    

        <?php foreach ($person as $key => $feature) : ?>

            <li><strong><?= $key; ?></strong> <?= $feature; ?></li>

        <?php endforeach; ?>

    </ul>
    
</body>
</html>
0 likes
13 replies
MichalOravec's avatar

Propably you don't have enable short_open_tag in your php.ini on our server

Try this

<li><strong><?php echo $key; ?></strong> <?php echo $feature; ?></li>
James_Moore's avatar

Hey @pt-83 I did the following just to try and replicate your error.

<?php

$person = [
    'age' => 31,
    'height' => 5,
    'profession' => 'unknown'
];
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <ul>

        <?php foreach ($person as $key => $feature) : ?>

            <li><strong><?= $key; ?></strong> <?= $feature; ?></li>

        <?php endforeach; ?>

    </ul>

</body>
</html>

I put it all in the same file test.php and it worked printing the

  • items. So I believe you may just be using the

    require 'index.view.php'; 
    

    incorrectly, double check that your require 'index.view.php' has the correct path to it, you may need to double check its not a require '../views/index.view.php'; or something I know jeffery will typically put views in their own directory.

    PT-83's avatar
    Level 7

    Hi @james_moore, I am getting the bullet list also but the problem is it's not passing the array data to the list for some reason.

    James_Moore's avatar

    @pt-83 Hey comment out the html markup

    <!-- html goes here -->
    

    and do a

    <?php var_dump($person) ?>

    to make sure $person is getting passed to the view to begin with

    PT-83's avatar
    Level 7

    @james_moore after var_dump nothing is happening and the 4 bullet list is still showing with no additional data. Also I noticed this on localhost Closed without sending a request; it was probably just an unused speculative preconnection not sure if that's causing an issue.

    I also edited the <?= $key; ?> to use <?php tags instead

    James_Moore's avatar

    make sure your php server is running as well :)) php -S localhost:8000

    PT-83's avatar
    Level 7

    I tried but nothing the same things occur. The bulleted list with no data from the array being passed. I have a funny feeling this is a localhost issue on my end. I will try and diagnose and see what I can come up with.

    nolros's avatar

    @pt-83 try this

        <ul>
          <?php foreach ($person as $key => $feature): ?>
            <li><strong><?php $key; ?></strong> <?php $feature; ?></li>
          <?php endforeach; ?>
        </ul>
    
    
    PT-83's avatar
    Level 7

    Hi all, thanks for your assistance. I am not sure what I did wrong here, but I moved on with the tutorial lesson and got it to work with different data. I did tinker with localhost which could have solved the issue also.

    Consider this closed.

    Thank you all.

    1 like

    Please or to participate in this conversation.