Home > OS >  Zero data when php data type is int
Zero data when php data type is int

Time:11-01

I wrote a function that takes the screen resolution and performs mathematical operations with PHP.

namespace example\standart;

class calc
{
    public $width, $height;

    function __construct(){
        $this->width = "<script>document.write(screen.width)</script>";
        $this->height = "<script>document.write(screen.height)</script>";
    }

    function findPerc($per)
    {
        $per = $per / 100;
        return $per * intval($this->width);
    }

    function findPercHeight($per)
    {
        $per = $per / 100;
        return $per * intval($this->height);
    }
}

Then I included my class on the page as you see below:

$calc = new example\standart\calc();

When I directly press the width value to the screen, I successfully see the result.

echo $calc->width;

But when I run my findPerc() function, I get 0 response. This is because of the intval() function. When I convert String data type to int data type with any function, I get 0 result. How can i solve this problem?

Thanks you.

CodePudding user response:

In your __construct() function you literally assign strings to $this->width and $this->height. These strings are just strings (although you expect them to run as JS code).

You can't get screen dimensions this way.

  • Related