Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: set variable to infinity

Tags:

php

I am wondering if I can set a variable to infinity, and if not what the best way to achieve my problem is. Take my function below:

public function seekValue($value, $column = null, $limit = null) {
  $this->connect('rb');
  $results = array();
  while (!feof($this->_pointer)) {
      $data = explode($this->_config->delimiter(), fgets($this->_pointer, 1024));
      if(!is_null($column)) {
          if ($data[$this->_config->columns($column, "string")->index()] == $value)
              array_push($results, $this->formatRow($data));
      } else {
          if (in_array($value, $data))
              array_push($results, $this->formatRow($data));
      }
  }
  $this->disconnect();
  switch (count($results)) {
      case 0;
          return false;
      case 1;
          return $results[0];
      default;
          return $results;
  }
}

I set $limit = null in the function parameter list, however I later want to use $limit in my while loop like so while (!feof($this->_pointer) && count($results) < $limit) incase the user decides to pass an integer to it.

If this was the case I could do this:

if (!is_int($limit)) {
  $limit = infinity;
}

To say that if $limit is not set run infinite times.

I hope this makes sense.

like image 999
George Reith Avatar asked Dec 12 '11 16:12

George Reith


People also ask

Is infinite function in PHP?

The is_infinite() function checks whether a value is infinite or not. This function returns true (1) if the specified value is an infinite number, otherwise it returns false/nothing. The value is infinite if it is outside the allowed range for a PHP float on this platform.

How to declare integer value in PHP?

PHP doesn't by default make a variable integer or string, if you want to set default value, then simply write $myvariable = 0; and this will assign and make variable an integer type.

How can check integer value or not in PHP?

The is_int() function checks whether a variable is of type integer or not. This function returns true (1) if the variable is of type integer, otherwise it returns false.

What are PHP numbers?

Definition and UsageThe is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.


1 Answers

Just to answer the original question:

Yes you can set a variable to infinity by assigning INF

$x = INF;
var_dump($x > 10000); // bool(true)
var_dump($x - 100); // float(INF)
like image 89
edorian Avatar answered Oct 19 '22 23:10

edorian