Darkaaja
Darkaaja

Reputation: 98

PHP Division by zero error where no math is present?

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);

Upvotes: 1

Views: 354

Answers (2)

Amal Murali
Amal Murali

Reputation: 76656

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.

Upvotes: 4

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

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.

Upvotes: 1

Related Questions