Not sure why the method is not returning any value
<p>I know I already have a similar post, but I can't for the life of me work out what is different between the two snippets - as I can only get the return value from the first example - but not the second example, both methods in the examples return a value. What is even more bizarre (or frustrating) is that php does not throw up any errors on the second example it simply presents a blank screen. I am running on XAMMP and I believe "show errors" etc. is set to true.</p>

<p>First example (works perfectly):</p>

<pre><code>&lt;html&gt;
    &lt;?php 
        function myMethod() {
            return "testing";
        }
    ?&gt;

    &lt;script type="text/javascript"&gt;
        var val= "&lt;?php echo myMethod(); ?&gt;";
        document.write(val);
    &lt;/script&gt;

&lt;/html&gt;
</code></pre>

<p>Second example: (Note only the 'myMethod' function has changed)</p>

<pre><code>&lt;html&gt;
    &lt;?php 
        function myMethod() {
            error_reporting(E_ALL);

            echo "&lt;h2&gt;Querying...&lt;/h2&gt;\n";

            /* Get the port for the WWW service. */
            $service_port = getservbyname('www', 'tcp');

            /* Get the IP address for the target host. */
            $address = gethostbyname('www.example.com');

            /* Create a TCP/IP socket. */
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            if ($socket === false) {
                echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
            } else {
                echo "OK.\n";
            }

            echo "Attempting to connect to '$address' on port '$service_port'...";
            $result = socket_connect($socket, $address, $service_port);
            if ($result === false) {
                echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
            } else {
                echo "OK.\n";
            }

            $in = "HEAD / HTTP/1.1\r\n";
            $in .= "Host: www.example.com\r\n";
            $in .= "Connection: Close\r\n\r\n";
            $out = '';

            echo "Sending HTTP HEAD request...";
            socket_write($socket, $in, strlen($in));
            echo "OK.\n";

            echo "Reading response:\n\n";
            while ($out = socket_read($socket, 2048)) {
                echo $out;
                return 'test';
            }

            echo "Closing socket...";
            socket_close($socket);
            echo "OK.\n\n";
        }
    ?&gt;

    &lt;script type="text/javascript"&gt;
        var val= "&lt;?php echo myMethod(); ?&gt;";
        document.write(val);
    &lt;/script&gt;
&lt;/html&gt;
</code></pre>

<p>Update: Further to what @the system said, it looks like the variable contains the correct data but "document.write" does not seem to pipe it out to the browser view properly:</p>

<pre><code>&lt;html&gt;
&lt;script type="text/javascript"&gt;
    var val= "&lt;h2&gt;Querying...&lt;/h2&gt;
    OK.
    Attempting to connect to '192.0.43.10' on port '80'...OK.
    Sending HTTP HEAD request...OK.
    Reading response:

    HTTP/1.0 302 Found
    Location: http://www.iana.org/domains/example/
    Server: BigIP
    Connection: close
    Content-Length: 0

    test";

    document.write(val);
&lt;/script&gt;
&lt;/html&gt;
</code></pre>
