Can anyone please tell me why line 1 works (returns 35434), but line 2 doesn't (returns blank)?
echo $_FILES['userfile']['size'];
die ($_FILES['userfile']['size']);
Thanks!
die is equivalent to exit and you'll notice that exit takes either an integer or a string as an argument. In the case you pass an integer, then the program exits and returns that integer as its exit code.
$_FILES['userfile']['size'] is an integer, not a string, so instead of outputting the message to the screen, it returns the size as the return code of the program.
A simple solution is to concatenate to an empty string to let the PHP compiler you want a string instead of an integer:
die('' . $_FILES['userfile']['size']);
I answered this a few hours ago, anyway the other answers are right.
As a workaround (if you need to do that), casting the integer to a string will do the trick:
die(strval($_FILES['userfile']['size'])); // or
die((string) $_FILES['userfile']['size']);
die() only prints strings. (See manual) If you add some text in there it will work, for some reason. ;)
Related
Why do I get output of 60 from die() for this:
$min = $row['min'];
die($min);
But not for this?
$min = $row['min']*60;
die($min);
Solution
If you need the value even if it's a number, parse it to string with strval:
die(strval($min));
Documentation
According to die, it's an Equivalent to exit.
In Exit we can see that it has two signatures:
void exit ( int $status )
And
void exit ([ string $status ] )
Both are described below:
If status is an integer, that value will be used as the exit status
and not printed. Exit statuses should be in the range 0 to 254, the
exit status 255 is reserved by PHP and shall not be used. The status 0
is used to terminate the program successfully.
And
If status is a string, this function prints the status just before exiting.
(emphasis mine)
Elaborating
In the first code example from your question, $row['min'] is most likely just a string (eg string(2) "70"). When you multiply it by a number, PHP 'casts' the value to a number so it can multiply.
So, in the first example, it's calling the exit ([ string $status ] ) function, which prints the status just before exiting.
In the second example, it's calling the exit ( int $status ) function, which will be used as the exit status and not printed.
If you want to see it for yourself, use var_dump and check the variables types. Check an Example.
It is because you should pass a string: "Required. Specifies the message or status number to write before exiting the script. The status number will not be written to the output."
After multiplying the value is casted to int type and therefore die will not print it.
The die() function is an alias of exit().
PHP's documentation states that exit() is polymorphic. That is, it can accept both strings or integers as its only argument.
When you provide a string, it is printed before the script exits.
When you provide an integer (between 0-254), it is used as the script's exit status code and nothing is printed. A code of 0 denotes successful completion of the script, and anything else is an error. The code 255 is reserved by PHP.
You can use strval() or intval() to ensure you are passing the right data type for your needs.
if i use:
socket_read($socket, 1024);
I don't know what type of variable it returns. I want to make it to an int, but i don't know how.
The Server respons only a number (35).
If i echo it, i get 35. If i cast it to "(int)" i get 0. intval returns 0 too.
I have tried to cast it to string, and pick the first letter (3) but it returned 0. Substr doesn't work too.
I want to have an int value, so that i can calculate things with this value. I need an int to calculate the prozentage of this value from another value.
What can i do?
EDIT:
Fixed it in JAVA SERVER SOCKET CODE whith:
PrintWriter out = new PrintWriter(server.getOutputStream());
instead of using
DataOutputStream out = new DataOutputStream(server.getOutputStream());
Why not read the manual page. It returns a string.
Try print_r to find out the details of that string. Might be in binary?!
As you can see in the manual, socket_read always returns a string. You should try using var_dump() to check the real value of the variable.
Did you try to sum any number to that variable? In the autocast it may work.
Seems like the string contains additional whitespace that prevents PHP from converting it to a number.
Therefore I suggest you use the trim() function to remove whitespace before converting it to a number. Or you could use a regular expression to remove all non-numeric characters $out = preg_replace("/[^0-9]/g", "", $in)
If this still doesn't work and var_dump doesn't help you either, I'd suggest to sniff the network traffic using WireShark to inspect the packet coming from the server. If you're in control of what the server sends, use a decent serialization method like XML or JSON instead of inventing some protocol yourself.
echo 246810121416182022242628303234363840;
such
then it shows me value on brower as
2.4681012141618E+35
Please suggest me solution
wrap it in ""
echo "246810121416182022242628303234363840"
Use number_format(). For example:
echo number_format(246810121416182022242628303234363840,0,null,'');
Also, you can define custom thousands separator.
When dealing with very long integers in PHP, you're going to have trouble regardless of what you try to do.
From PHP's docs
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead.
When given an int with value 246810121416182022242628303234363840, PHP interprets it as a float with value 2.4681012141618E+35. Even attempting to use number_format() as Timur suggests will result in printing '246810121416182028912708947168722944'.
Given that PHP can't be passing that value as you describe, you must be typing it in your code? If this is the case, your best bet for accurate echoing is to create a string instead of an int.
I noticed this weird evaluation yesterday after searching for a few hours in my code for an error. i am passing scores into php, sometimes the score=0 which causes an issue.
send php ?blah=blah&score=0
if(!empty($_REQUEST['score']){
//do database update stuff
}else{
// show entire webpage
}
It works great unless the score=0 the if() will evaluate to false and return the entire webpage to my ajax handler and error. I have temporarily changed !empty to isset but this will cause problems in the future because isset evaluates to true even if the score key is in the url string without a value.
ex: (?blah=blah&score=&something=else)
my question is: what is the best way to recode this to work correctly now and in the future?
edit: there are a few working answers here, i appreciate everyones time. it was difficult to choose an answer
As the manual says, a variable is considered empty() if it has an empty or zero value.
So it will treat your variable wrongly as empty even though 0 is a perfectly acceptable value in your case.
If you need score to be a number, you could use isset() in combination with a is_numeric() check instead:
if((isset($_REQUEST['score']) and (is_numeric($_REQUEST['score'])){
Check out the manual page to see the kinds of values is_numeric() accepts. If score is always an integer, you can also use is_int((int)$_REQUEST['score']) but that will convert invalid input values to 0.
Additionally, as #sightofnick says, it's better to use explicit $_GET or $_POST instead of $_REQUEST.
Re your update:
In that case I would
Do check whether the variable is "0" (string "zero")
If it is "0", make it 0 (integer "zero")
If it is not 0, convert it to an integer (int)$_REQUEST["score"])
If the conversion resulted in 0, it was invalid input - exit
You have a valid integer variable.
empty() will return false if a value is zero. Use isset() or array_key_exists() instead, if you want to check if a variable in an array is set:
if (array_key_exists('score', $_REQUEST)) {...}
Try doing
if (isset($_REQUEST['score']) && ($_REQUEST['score'] !== '')) {
...
}
The isset will handle the presence/absence of the query parameter, and the strict string (!==) comparison will handle the case where the 'score' query is present but has no value. PHP treats all data coming from _GET/_POST/_REQUEST as strings, so this test is 100% reliable.
if(isset($_REQUEST['score']) && $_REQUEST['score'] != ''){
//do database update stuff
}else{
// show entire webpage
}
You may be able to solve that with
if (isset($_REQUEST['score']) && is_numeric($_REQUEST['score'])) {}
That of course if scrore can only contain numeric value
We are currently migrating from PHP4 to PHP5 and have discovered an issue.
In PHP4, when we used the number_format() function on an empty string the output would be 0. However, in PHP5, running the same empty string through the number_format() function results in output of NULL. This has a ripple effect on a lot of our code that is not expecting NULL.
Is there a best-practice solution for this? Ideally I'd like to be able to make the change at the number_format() call so that empty strings return 0 instead of NULL and not have to check all the possible places where the output may be used.
Why not just check for an empty var before you pass it to number_format?
number_format(empty($var) ? '0' : $var);
Empty isn't the only issue. Usually, any non-numeric should be 0.
number_format(is_numeric($var) ? $var : 0);
will render anything not a number to be zero.