2

I have a code where it runs fine on phpFiddle.org, but when I tried to run it on my web server, it displayed an internal web error for some reason. I'm wondering what I did wrong. Thanks.

    $text = "cheese\'s bacon cats ";
    $replacement = ["cheese\'s" => "bacon", "bacon" => "apple", "cats" => "dogs"];
    $search = array_map(function($v){
        return preg_quote($v, "/");
    }, array_keys($replacement));

    echo $text = preg_replace_callback("/\b(" . implode("|", $search) . ")\b/", function($m)use($replacement){
        return $replacement[$m[1]];
    }, $text);

P.S: My web server is ipage.com

Here's the edited version of the code:

$text = "apple\'s bacon cats ";
    $replacement = array("apple\'s" => "bacon", "bacon" => "apple", "cats" => "dogs");
    $search = array_map(function($v){
        return preg_quote($v, "/");
    }, array_keys($replacement));

    echo $text = preg_replace_callback("/\b(" . implode("|", $search) . ")\b/", function($m)use($replacement){
        return $replacement[$m[1]];
    }, $text);
frosty
  • 2,559
  • 8
  • 37
  • 73
  • Do you have error reporting turned on for your application? http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – Maximus2012 Sep 04 '15 at 18:28
  • What error do you get? – Rizier123 Sep 04 '15 at 18:29
  • @Rizier123 Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. – frosty Sep 04 '15 at 18:30
  • @frosty Have you looked into the server logs? – Rizier123 Sep 04 '15 at 18:30
  • BTW: My code only runs with PHP version >=5.4 – Rizier123 Sep 04 '15 at 18:33
  • @Rizier123 Is there a way to change it around a bit to make it work with 5.3? – frosty Sep 04 '15 at 18:35
  • @frosty Yes, see the posted answer below. Also the comment, which you posted under it is correct – Rizier123 Sep 04 '15 at 18:35
  • @Rizier123 I just tried to edit my codes with the array include, but it's still not working properly for some reason. I've updated my code above with my edited version. Please take a look at it. – frosty Sep 04 '15 at 18:39
  • @frosty As already said look into your error log and tell us what errors are in there. Also make sure you have error reporting turned on: `ini_set("display_errors", 1); error_reporting(E_ALL);` – Rizier123 Sep 04 '15 at 18:41

2 Answers2

3

iPage PHP version is currently 5.3. The array notation you are using, [] is only supported as of PHP 5.4. Switch all of your array code to array() and you should be fine. It is important when using hosting services such as ipage that you check consistencies between versions of your local development environment and the server.

samrap
  • 5,595
  • 5
  • 31
  • 56
  • Like this? array("apple\'s" => "bacon", "bacon" => "apple", "cats" => "dogs"); – frosty Sep 04 '15 at 18:34
  • It doesn't work. I must still be missing something. Please take a look at my edited version of the code above. – frosty Sep 04 '15 at 18:40
  • closures are supported as of 5.3, so that shouldn't be your issue. does ipage give you access to error logs? – samrap Sep 04 '15 at 18:46
  • It seems I was running at php 5.2. That's why. – frosty Sep 04 '15 at 18:48
  • Ah, so are you saying ipage uses 5.2 and not 5.3? I can update my answer if that is the case – samrap Sep 04 '15 at 18:49
  • It supports 5.2, 5.3, and recently 5.5. I'm going to switch over to 5.3. – frosty Sep 04 '15 at 18:51
  • Since we're discussion PHP versions, I want your opinion on what version you think we should generally use as of now. – frosty Sep 04 '15 at 18:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88831/discussion-between-samrap-and-frosty). – samrap Sep 04 '15 at 18:53
2

In addition to samrap's notes, you should use https://3v4l.org/ instead of phpfiddle. 3v4l is much more feature-filled and will show you the results of you code in several different php versions instead of assuming you're running whatever version pf php that phpfiddle is using. In addition, 3v4l will show you performance stats, etc.

samrap
  • 5,595
  • 5
  • 31
  • 56
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • I didn't even know about 3v4l. Great resource for compatibility checks! – samrap Sep 04 '15 at 18:40
  • I don't really see how this should answer OP's question? – Rizier123 Sep 04 '15 at 18:40
  • 1
    @Rizier123 OP's question was about his PHP code not working on the server, which happened to be because of version differences. This answer gives the OP a way to prevent other iterations of the same problem – samrap Sep 04 '15 at 18:41
  • Yes! A better site then phpFiddle to test my codes on. – frosty Sep 04 '15 at 18:41
  • @frosty - samraps's is the correct answer, mine is just a supplemental note that i felt worthy of posting as an answer as well, but you should hit the check mark by his answer. – I wrestled a bear once. Sep 04 '15 at 18:45
  • I appreciate it @frosty, but now it seems like he still has another issue. My answer definitely solves a main part of the problem, but he is still having issues – samrap Sep 04 '15 at 18:47