0

I have a PHP application with me, which was done by myself and a few of us. I have not coded much, but it worked well in the localhost. When I tried to upload it in our university web server, I had got this error.

Parse error unexpected :

This happened on this line. So I believe that PHP has to do something with respect to the previous line too. So I am adding the previous and next lines:

<?php
  session_start();
  $page = $_GET["page"] ?: "index"; // Error in this line!

The funny part is, this works on my WAMP Server locally, but it doesn't work in the university server. Is there any issue with the code?

6
  • 3
    Your running a newer version localy - the ?: syntax is new
    – Philipp
    Commented Oct 16, 2015 at 20:25
  • 1
    What's the PHP version locally and in the server? Commented Oct 16, 2015 at 20:26
  • I am using PHP 5.4 in WAMP Server. University uses 5.2.12.
    – user5105916
    Commented Oct 16, 2015 at 20:27
  • you have to upgrade it to 5.3
    – Bak
    Commented Oct 16, 2015 at 20:36
  • 1
    Talk to the admins. If they don't hear there is a need for an update, they won't do anything. Having such an outdated PHP version will create plenty of problems when using current PHP software, not only related to security.
    – Sven
    Commented Oct 16, 2015 at 20:43

4 Answers 4

3

I believe the PHP in your University Web Server is very old or older than 5.3. This is a shorthand ternary operator and is supported by PHP versions 5.3 and above.

Workaround

$page = $_GET["page"] ? $_GET["page"] : "index";

Update: To remove the warning, where $_GET["page"] is not set, you can use:

$page = isset($_GET["page"]) ? $_GET["page"] : "index"; // Checks if $_GET["page"] exists, and then assigns it.

PHP 7 will allow to use this short syntax:

$page = $_GET["page"] ?? "index";

From the docs:

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

6
  • The workaround worked. But I have an issue. I am sometimes getting Warning in the index page alone. Is there anything that you can help me with?
    – user5105916
    Commented Oct 16, 2015 at 20:29
  • That could be because $_GET["page"] is not set. You can override it with an @ in front of it. Commented Oct 16, 2015 at 20:30
  • Don't suppress errors, it's bad practice! Rather, do a test if the field is set (isset($_GET['page'])) ? //do stuff : //do other stuff
    – al'ein
    Commented Oct 16, 2015 at 20:35
  • 1
    @AlanMachado I have included that in my comment in the line. Did you see that? :) Commented Oct 16, 2015 at 20:36
  • Yes, I saw it. I was making a suggestion, also!
    – al'ein
    Commented Oct 16, 2015 at 20:37
0

Quoting from the PHP Docs

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

I suggest that your university should update their servers to a supported version of PHP

1
  • Can't upgrade the university servers, sorry. But thanks, I got a workaround from Praveen.
    – user5105916
    Commented Oct 16, 2015 at 20:31
0

The problem may be the version of PHP.

Maybe your PHP version local is higher than in your university server.

You must see in which version from PHP ?: works

0
-1

If the syntax does not work on your server it might be that "short tags" are disabled in your PHP ini file.

1
  • Short tags are enabled.
    – user5105916
    Commented Oct 16, 2015 at 20:29