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 the PHP interpreter does not throw up any errors on the second example; it simply presents a blank screen. I am running on XAMPP and I believe “show errors”, etc. is set to true.
First example (works perfectly):
<html> <?php function myMethod() { return "testing"; } ?> <script type="text/javascript"> var val= "<?php echo myMethod(); ?>"; document.write(val); </script> </html>
Second example (note only the ‘myMethod’ function has changed):
<html> <?php function myMethod() { error_reporting(E_ALL); echo "<h2>Querying...</h2>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.1rn"; $in .= "Host: www.example.comrn"; $in .= "Connection: Closernrn"; $out = ''; echo "Sending HTTP HEAD request..."; socket_write($socket, $in, strlen($in)); echo "OK.n"; echo "Reading response:nn"; while ($out = socket_read($socket, 2048)) { echo $out; return 'test'; } echo "Closing socket..."; socket_close($socket); echo "OK.nn"; } ?> <script type="text/javascript"> var val= "<?php echo myMethod(); ?>"; document.write(val); </script> </html>
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:
<html> <script type="text/javascript"> var val= "<h2>Querying...</h2> 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); </script> </html>
Advertisement
Answer
Have a look at the source code view of your browser. The script seems to work as you may expect (see below).
But note that in JavaScript it is not possible to build multiline strings this way.
When building a multiline string in JavaScript you’ll have to do this:
var str = "first linen"; str += "second linen";
Or use the join function:
var str = [ "first line", "second line" ].join("n");
Here comes your output:
<html> <script type="text/javascript"> var val= "<h2>Querying...</h2> 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); </script> </html>