1
Warning: Division by zero in ..\session.php on line 2

After updating my PHP to the most current version my host allows (5.5.9) there was a small issue with the session path so I had to include a small snip-it of code before the session_start() to fix it. However this odd error is happening, apparently it thinks I am attempting to divide by zero? I would like to know how to fix that if it is possible, I assume it is just PHP being stupid however.

Thanks! :)

Below is the first few lines of code

<?php
session_save_path(“/tmp”);
session_start();
//error_reporting(0);

EDIT: Fixed the issue using the code below

session_save_path(realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session'));
ini_set('session.gc_probability', 1);
Darkaaja
  • 98
  • 8

2 Answers2

4

It's the difference in the quotes used. PHP thinks you're trying to divide a string by another string. Change that to regular double-quotes and the problem will be resolved.

Right now, this is what PHP sees:

“/tmp”
^--- first string
 ^--- division operator
  ^^^^--- second string

The string is cast to an int before the operation, and that effectively turns both sides to 0, hence causing the Division by zero warning. You'd have caught this if you had enabled error reporting.

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • 1
    Increasing the error reporting level would have helped to identify this: http://codepad.org/XaJcv1xo – cmbuckley Feb 09 '14 at 19:52
  • very well explained one up from me :) – Abhik Chakraborty Feb 09 '14 at 19:52
  • How would I do this without the use of 'smart' quotes as I attempted already to swap for normal quotes and that only ruins the fix. (Noob PHP coder) – Darkaaja Feb 09 '14 at 19:55
  • @Darkaaja: By smart, I assume he meant irregular quotes. **`”` is different from `"`**. The command should use proper quotes: `session_save_path("/tmp");` - please explain how normal quotes "ruin your fix". – Amal Murali Feb 09 '14 at 19:58
  • @Darkaaja Smart quotes mean nothing to PHP, as this code shows, so if they appeared to be fixing a problem, something else was happening. Since division by zero still returns something (boolean false), what you are actually doing is `session_save_path(false);` which will in turn be the same as `session_save_path('');` but I'm not quite sure what that will actually do. – IMSoP Feb 09 '14 at 20:01
1

You have smart quotes.

Your code is therefore being interpreted as:

  • Unquoted string literal
  • Divide by...
  • Unquoted string literal tmp“

Division casts to numbers, both sides get cast to zero, result is 0/0.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • How would I do this without the use of 'smart' quotes as I attempted already to swap for normal quotes and that only ruins the fix. (Noob PHP coder) – Darkaaja Feb 09 '14 at 19:52
  • This is indeed what is happening, and becomes clearer with Notices enabled, as shown in this demo: http://3v4l.org/iaquX – IMSoP Feb 09 '14 at 19:52