سؤال

Google Chrome 500 Error

Okay, so I was playing around with the idea of a stack overflow loop. I entered the following code and got a cute little image in Google Chrome (their answer for a 500 internal error which is NOT helpful by the way Google!). This was as expected and on purpose.

Code Set #2

<?php
    for($x=-1;$x<=3;$x++){
     echo $x/0.">";
    }
?>

By checking the headers I found:

http://server.domain/overflow.php

GET /overflow.php HTTP/1.1
Host: server.domain
User-Agent: Browser
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cache-Control: max-age=0

HTTP/1.0 500 Internal Server Error
Date: Thu, 22 Aug 2013 21:51:30 GMT
Server: Apache
X-Powered-By: PHP/5.3.3
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8

I arrived at the above code because I was actually wondering about dividing by zero and how PHP handled it. I wrote the code below to try and trigger the result, but didn't get what I was expecting. The problem is rather than getting a 500 internal server error from the following code, I get something else... a NULL where I would expect the server to throw the error.

Code Set #1

<?php
    for($x=-1;$x<=3;$x++){
     echo $x/$x.">";
    }
?>

Output

1>>1>1>1>

Question

Why isn't the first bit of code causing a 500 internal server error since I'm dividing by zero? 1/1=1,1/0=500 Error,0/0=Null

هل كانت مفيدة؟

المحلول

You get an error with the first version, because you have a syntax error. PHP treats the dot in 0. as a decimal point, not a concatenation operator. The correct code would be either:

($x/0) . ">"; // This version is my preference

Or:

$x/0. . ">";

نصائح أخرى

You should get

Parse error: syntax error, unexpected '">"' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in /your_file.php on line 3

Changing your code to

<?php
    for($x=-1;$x<=3;$x++){
     echo $x/0;
     echo ">";
    }
?>

will get you

Warning: Division by zero in /your_file.php on line 3
>
Warning: Division by zero in /your_file.php on line 3
>
Warning: Division by zero in /your_file.php on line 3
>
Warning: Division by zero in /your_file.php on line 3
>
Warning: Division by zero in /your_file.php on line 3
>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top