exp() returns NaN stuff - php

Ok, I'm stuck. I have this PHP code:
echo exp(12), '<br/>';
echo exp(4.2);
just like on the PHP.net page. And what I have on the screen:
NAN
298.86740096706
but then there comes the weirdest thing ever. I thought that maybe there's some PHP.ini problem with numbers like 2.67e8 and so (I mean with the 'e' inside) or something. But then, when I changed the above code into:
echo (2.67e8), '<br/>';
echo exp(4.2);
suddenly I saw this whatever:
267000000
NAN
It's kind of WTF especially because of the last NAN, when first it was a quite normal, calm float 298.86740096706 but then just with no reason it went to hell replaced by NAN. Do you have any ideas? Please?

If true, this must be a bug in your PHP version. Please report it.
By the way, I'm unable to reproduce it on PHP 5.3.2. Running:
php -r 'do { $c = exp(4.2); echo "point "; } while (is_nan($c)); echo "$c\n";'
produces the expected output:
point 66.686331040925

Related

Has PHP 7.4 broken fgets(STDIN)?

I have just upgraded from PHP 7.3.12 to PHP 7.4.0 (released today) on Windows.
This worked until just now:
<?php
$input = fgets(STDIN);
var_dump($input);
It now outputs:
bool(false)
It doesn't ask/allow for input at all anymore. It immediately returns a bool false.
I can't find any mention of any recent changes to fgets in either the changelog or the manual page.
What is wrong? What am I supposed to do? Is this a bug? Is it known? Has it been encountered by anyone else?
Also, if this is wrong somehow (in spite of working for so long, and in spite of me finding this code recommended online), what is the "real" way to accept user input/wait for Enter?
I have now downgraded back to 7.3.12 for the moment to fix this issue.
EDIT: Somebody has finally submitted a bug report for this. I sure hope it won't be ignored, as is so often the case in many FOSS projects: https://bugs.php.net/bug.php?id=78883
Confirming that I'm experiencing the same behavior with 7.4. I created a kludgy workaround for now:
ob_start(); // buffer so we don't see the output generated at DOS prompt
$cmd_line='SET/P phpinput= & SET phpinput'; // Step 1: prompt user to enter a value for variable phpinput & Step 2: display the value for phpinput
$result=system($cmd_line); // Execute
$result=str_replace('phpinput=', '', $result); // Clean up the returned result
ob_end_clean(); // resume normal output
echo "\nReturned result from user typing is: $result\n";

Strange behaviour from PHP. var_dump(390.0) outputs 38:

Test file:
<?php
$var = 0.28682926829268;
$roundedVar = round($var, 2);
var_dump($roundedVar);
var_dump(is_float($roundedVar));
echo "Rounded var: $roundedVar";
?>
Output:
float(0.28:)
bool(true)
Rounded var: 0.28:
Note the strange colon character that appears after 0.28 both in the var_dump and in the echo output.
After discovering that, I figured PHP must be corrupted somehow. So I uninstalled and reinstalled PHP. I reinstalled the same version of PHP, as detailed below. I re-ran the test code and got the same result
I then reinstalled both PHP and Apache 2.2. After doing that the test case above worked as expected, but then another error turned up, that I reduced to the following test case.
Test file:
<?php
$var = 390;
echo "var: " ; var_dump($var );
echo "is_float(): " ; var_dump(is_float($var) );
echo "is_string(): "; var_dump(is_string($var));
echo "echo: $var<br/>\n";
$var = 390.0;
echo "var: " ; var_dump($var );
echo "is_float(): " ; var_dump(is_float($var) );
echo "is_string(): "; var_dump(is_string($var));
echo "echo: $var<br/>\n";
?>
Output:
var: int(390) is_float(): bool(false) is_string(): bool(false) echo: 390
var: float(38:) is_float(): bool(true) is_string(): bool(false) echo: 38:
Can anyone explain this strange behavior?
Updates:
I found by running hardware diagnostics that the the CPU on the server was overheating. When that problem was fixed so that the hardware diagnostic passed, the error stopped appearing in the logs (see the accepted answer below).
For anyone else investigating a similar problem, I found discussion of another weird PHP bug involving floating-point numbers and colon suffixes at the following URL. Not sure whether related.
https://bugs.php.net/bug.php?id=51396
PHP version info:
The CPU was faulty.
I downloaded an Intel CPU diagnostic tool from here:
https://downloadcenter.intel.com/download/19792/Intel-Processor-Diagnostic-Tool
Relevant output:
Current Junction Temperature is 99C
Current Degrees Below Max 1C
Maximum Junction Temp Allowed 100C
Test Result - FAIL
Please check your Processor Thermal Solution

PHP strpos() not working

I am trying to get PHP to search a text file for a string. I know the string exists in the text, PHP can display all the text, and yet strpos returns false.
Here is my code:
<?php
$pyscript = "testscript.py";
//$path = "C:\\Users\\eneidhart\\Documents\\Python Scripts\\";
$process_path = "C:\\Users\\eneidhart\\Documents\\ProcessList.txt";
//$processcmd = "WMIC /OUTPUT: $process PROCESS get Caption,Commandline,Processid";
$process_file = fopen($process_path, "r") or die("Unable to open file!");
$processes = fread($process_file);
if (strpos($processes, $pyscript) !== FALSE) {
echo "$pyscript found";
} elseif (strpos($processes, $pyscript) === FALSE) {
echo "$pyscript NOT found :(";
} else {
echo "UHHHHHHHH...";
}
echo "<br />";
while (!feof($process_file)) {
echo fgets($process_file)."<br />";
}
fclose($processfile);
echo "End";
?>
The while loop will print out every line of the text file, including
python.exe python testscript.py
but strpos still can't seem to find "testscript.py" anywhere in it.
The final goal of this script is not necessarily to read that text file, but to check whether or not a particular python script is currently running. (I'm working on Windows 7, by the way.) The text file was generated using the commented out $processcmd and I've tried having PHP return the output of that command like this:
$result = `$processcmd`;
but no value was returned. Something about the format of this output seems to be disagreeing with PHP, which would explain why strpos isn't working, but this is the only command I know of that will show me which python script is running, rather than just showing me that python.exe is running. Is there a way to get this text readable, or even just a different way of getting PHP to recognize that a python script is running?
Thanks in advance!
EDIT:
I think I found the source of the problem. I created my own text file (test.txt) which only contained the string I was searching for, and used file_get_contents as was suggested, and that worked, though it did not work for the original text file. Turns out that the command listed under $processcmd creates a text file with Unicode encoding, not ANSI (which my test.txt was encoded in). Is it possible for that command to create a text file with a different encoding, or even simpler, tell PHP to use Unicode, not ANSI?
You can use the functions preg_grep() and file():
$process_path = "C:\\Users\\eneidhart\\Documents\\ProcessList.txt";
$results = preg_grep('/\btestscript.py\b/', file($process_path));
if(count($results)) {
echo "string was found";
}
You should follow the advice given in the first comment and use either:
file_get_contents($process_path);
or
fread($process_file, filesize($process_path));
If that fix is not enough and there is actually a problem on strpos (which shouldn't be the case), you can use:
preg_match("/.*testscript\.py.*/", $processes)
NB: Really try to use strpos and not preg_match as it's not advised by the documentation.
Well, I found the answer. Thanks to those of you who suggested using file_get_contents(), as I would not have gotten here without that advice. Turns out that WMIC outputs Unicode, and PHP did not like reading that. The solution was another command which converts Unicode to ANSI:
cmd.exe /a /c TYPE unicode_file.txt > ansi_file.txt
I hope this helps, for those of you out there trying to check if a particular python script is working, or if you're just trying to work with WMIC.

Sending value from R to php

I have this code in the .php file:
$nb1 = 1;
$nb2 = 2;
exec("C:/xampp/phpR/plotR.r $nb1 $nb2", $response);
echo $response;
And I have in the .r file:
args <- commandArgs(TRUE)
x<-args[1]+args[2]
print(x)
The php code prints "array" on browser. It should print "3". It prints "array". Where are the problem?
Thanks
PHP is interpreting the response from your R script as an array, whether the R script is actually returning an array I'm not sure. Regardless, if you do var_dump($response) you'll likely get something like
array (size=1)
0 => int 3
I think you should be able to substitute echo $response[0]; for echo $response; and get the results you need.
If anyone else happens upon this unanswered question, I had the same problem, and it turned out to be a permissions issue. The user that my webserver was running as didn't have permission to run my R script.
This can be diagnosed by following the excellent directions at:
How to retrieve PHP exec() error responses?
and viewing the error.
To get things to work nicely, I also changed the output of my R script from print(x) to write(x, stdout()). While print(x) would give you [1] 3 as an output, write(x) would return 3.

python 2.6.5, and getting the result of a call to a php app

i've got this snazzy python code:
import subprocess
value = subprocess.Popen(["php","./php/php_runner.php"],stdout=subprocess.PIPE);
the problem is, i have no idea how to check if the php_runner, well, ran. Currently, it has the following salient sections:
if (count($argv) != 4){
die("four arguments are needed\n");
}
and
$returnValue = call_to_another_php_class();
return $returnValue;
So what i want is this:
How do i get the return value, whatever it may be, using python?
You probably are going to tell me to use "PIPE" in the answer, but the (to me, incomprehensible) python docs (http://docs.python.org/library/subprocess.html) state:
Do not use stdout=PIPE or stderr=PIPE with this function. As the pipes are not being read in >the current process, the child process may block if it generates enough output to a pipe to fill up >the OS pipe buffer.
So what do i use then, because while I don't really know what they're barking on about, i sit up and take note about notes in grey boxes. Pity they didn't spell out what i'm meant to do - but, well, what am i meant to do?
the "returnValue" that my php code returns, is that what python is going to pickup as the return value from the function? If not, how do i return that value?
cheers!
UPDATE
Thanks to the given answer, here's the changes i made:
edited /etc/php5/cli/conf.d/mcrypt.ini (actually, this is just a change for ubuntu 10.04, and I changed the first line to begin with a ; instead of a #. That stopped an annoying "we don't like #" error that kept popping up)
in my php, I changed the code to read:
if (count($argv) != 4){
fwrite(STDERR, "four arguments are needed\n");
exit(1); // A response code other than 0 is a failure
}
this puts my error value as an error. the die() command wasn't doing that for me.
changed the python to read:
value = subprocess.Popen(["php","./php/php_runner.php"],stdout=subprocess.PIPE,
stderr=subprocess.PIPE);
print value.communicate();
Yeah, realistically, i'd do an if on value.communicate()[1], becase that is where the errors are.
$returnValue = call_to_another_php_class();
if ($returnValue == 1){ //hah, php has a good return value as 1.
//no problem
} else {
fwrite(STDERR,get_error_from_php_class());
exit(1);
}
booyah!
Since you're using the Popen constructor rather than the call functions, those notes about PIPE don't apply to you.
Use .communicate() as documented to wait for the program to finish and get the output.

Categories