-1

I have this simple function

  function render($template, $values = [])
{
    // if template exists, render it
    if (file_exists("../templates/$template"))
    {
        // extract variables into local scope
        extract($values);

        // render header
        require("../templates/header.php");

        // render template
        require("../templates/$template");

        // render footer
        require("../templates/footer.php");
    }

    // else err
    else
    {
        trigger_error("Invalid template: $template", E_USER_ERROR);
    }

This works fine when I am working on my localhost, but when I uploaded the files on my webhost and this function is used in a php file as follows->

<?php

   // configuration
   require("../includes/config.php"); 

   render("mainpage_template.php", ["title" => "Welcome "]);




   ?>

I get this parse error written in the title.Why is it working only on my localmachine?

Anoop Kanyan
  • 618
  • 7
  • 19
  • 3
    What version of PHP are you running on the webhost? Literal array notation was added in 5.4, so `[]` won't work in 5.3. – Barmar Dec 31 '13 at 22:52
  • on which line are you missing the []? – Eisa Adil Dec 31 '13 at 22:53
  • Can you paste the whole error that you received? By the way, `require` isn't a function, you doesn't must add brackets. –  Dec 31 '13 at 22:53
  • 1
    @VladGincher:`echo`, `print` also arent functions, but you can still add brackets. Don't teach people wrong stuff. – Glavić Dec 31 '13 at 23:01
  • @Glavić, I doesn't said that he can't, I said that he doesn't have to use them. Maybe I said the wrong word, I mean to the `(` and `)`. –  Dec 31 '13 at 23:04

1 Answers1

1

Are you using php 5.4 on your local? the render line is using the new way of initializing arrays. Try replacing ["title" => "Welcome "] with array("title" => "Welcome ")

hanleyhansen
  • 6,304
  • 8
  • 37
  • 73