-1

I am including a file in one of my other php files by doing this:

include(dirname(__FILE__) . '/send_notification.php');

The code of the file I am including looks like this:

<?php
echo "Here!";
if(false === function_exists('sendNotification')) {
    function sendNotification() {
        echo "I HATE THIS";
    }
}
echo "There!"
?>

The File is included as follows:

include_once(dirname(__FILE__) . '/send_notification.php');
echo "And Here!";

But its only echoing this:

Here!There!

What is going on?

  • Is there an error or warning showing up ? – Simon Duflos Sep 01 '15 at 08:50
  • Perhaps you do not SEE the text. By include you output it on the first line. You may call the function in a point of the script and the data is echoed in a place you do not see it (under a div perhaps). Check the text in the source. – Cosmin Sep 01 '15 at 08:50
  • To view the errors, add this at the very top of both your files and run it again: `ini_set('display_errors', true); error_reporting(E_ALL);` – laurent Sep 01 '15 at 08:51
  • i don't think this i good practice, your function should return the string, and then you should make a echo sendNotification(); The only reason to directly echo text in an include file is for displaying headers etc, but then it's not in a function – Doktor OSwaldo Sep 01 '15 at 08:51
  • I have tried to show the errors, but I am calling this PHP file from an Android device. So it does not output anything. –  Sep 01 '15 at 08:52
  • Where is the server located, on the android device? This is some very simple code that should just work, so i think there's some other issue going on. – laurent Sep 01 '15 at 08:55
  • Its a windows server that I bought. Somewhere in switzerland. –  Sep 01 '15 at 08:56
  • 1
    Maybe your server is not accessible at all from outside? What happens if you ping the domain or ip? – laurent Sep 01 '15 at 08:57
  • Well it works well when I just do the echo in the second file. Its just when I add the function that it fails to include it. Also I just pinged it. Its available. –  Sep 01 '15 at 08:58

2 Answers2

1

try this

<?php
    function sendNotification() {
        return "I HATE THIS";
    }
?>

echo sendNotification();
user1844933
  • 3,296
  • 2
  • 25
  • 42
1

You should always wrap your function to check for it's existence to prevent issues when included. And remove the closing PHP tag ?> from included files, to prevent end-of-line (EOL) lexing issues.

send_notification.php

<?php
if (false === function_exists('sendNotification')) {
    function sendNotification() {
        echo "I HATE THIS";
    }
}

index.php

<?php
require_once __DIR__ . '/send_notification.php';
sendNotification();
//...

You should also only use include_once or require_once notation on function/config pages to prevent overriding/duplication of variable values/functions when used.

Will B.
  • 17,883
  • 4
  • 67
  • 69
  • I have added the include_once. Check the updated question. I is still not working. –  Sep 01 '15 at 09:14
  • 1
    Your updated question doesn't show you calling `sendNotification()` – Will B. Sep 01 '15 at 09:15
  • Yeah because it seems that that is not the issue. It does not even get to the third echo statement. Something must be going wrong before that. –  Sep 01 '15 at 09:16
  • 1
    remove the end `?>` from the send_notification.php. I am guessing an `EOL` issue. – Will B. Sep 01 '15 at 09:17
  • Jesus Christ. Thank you my friend. That fixed it. What is EOL? Did it insert the other php file and just end the one that I was inserting into? –  Sep 01 '15 at 09:19
  • 1
    EOL is end of line. The issue is that you had an extra end of line character at the end of the script causing parsing issues. eg `?>\r\n` removing the end `?>` prevents PHP from parsing the `\r\n` as content and assumes it is PHP code. – Will B. Sep 01 '15 at 09:20