0

On my website, I have partners pages, each of which contains a PHP block defining array variables which are then passed to a loop that renders the content of the page. For example, for the hotels.php page, the PHP block is the following:

<?php
    $hotels = [
        ["href" => "link1", 
         "src" => "src1", 
         "alt" => "alt1", 
         "name" => "name1"
        ],
        ["href" => "link2", 
         "src" => "src2", 
         "alt" => "alt2", 
         "name" => "name2"
        ],
        ...
    ];
?>

Each group of partners page has a seperate php block. Since the code of each page is pretty much the same, with only difference being the PHP blocks and the way I refer to the variables ($hotels, $restaurants, etc.) I would like to merge them into one. I would merge the PHP into one and then use the URL to find out which pages I am on and figure out which variable to use to render the page.

I could do that, the problem is that if I am visiting the hotels' page, for example, I would only be using the $hotel variable and the other ones for the different types of partners would be declared but not used.

I would like to ask if the presence of unused variables would slow down the website and if there is a way of avoiding that problem (for example making the php code lazy - variables being declared only when they are called).

2
  • What about a class with static variables? Commented Sep 17, 2019 at 14:24
  • @NVRM What about it? Commented Sep 17, 2019 at 15:14

1 Answer 1

1

Make a template like this:

template.php

<ul>
    <?php foreach ($entities as $entity): ?>
        <li><?= htmlspecialchars($entity['name']); ?></li>
    <?php endforeach; ?>
</ul>

Then on your individual pages, define different $entities and include the reusable template:

hotels.php:

<?php

$entities = [...];
require 'template.php';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.