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.
Related
A newbie question.
Here is a function and an if statement.
<?php
function test(){
echo "somedata";
}
if(test())
?>
This code produces:
somedata
According to documentation an if expression should only return TRUE or FALSE, but apart of it, function runs. Why it happens?
PHP evaluates the if expression in order to know if the epression yields a truthy value or not. There is no other way to find out than to execute the functions that appear in the expression.
In this example it is very clear:
if (sqrt(4) > 1)
PHP must of course call the function sqrt, except that in this case it is a function without side-effects, while your function test also performs an echo. Secondly, it returns a value, while test doesn't. But that PHP can only find out by executing the function.
Note that the expression can in general return anything, but PHP has rules to what it considers truthy or falsy:
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource).
In case a function runs without executing a return statement, the returned value is null, so the if condition will be falsy according to the above rules.
I have written this code below which looks to see if a client is of a certain state and then exit if it is of a certain value. I have tested the query and syslog all variables being passed in to make sure they are not empty however, the return value of the exec command is 1. and $out is empty.
exec('/usr/bin/mysql --defaults-extra-file=database-user.cnf my_db -e "SELECT client_state FROM my_table WHERE transfer_id = '.$transfer_id.' AND client_id = '.$cid.'"', $out, $ret);
if($out == 15)
{
syslog(LOG_INFO, "Transfer to client ".$client->getName()." has already been completed on this transfer")
exit(EXIT_OK);
}
Beacause the output is empty it is skipping my if statement, any ideas why this command does not work and why the output is empty.
Look at the signature of the exec function:
string exec ( string $command [, array &$output [, int &$return_var ]] )
The $out variable (if your command is correct) will be an array, even if there's only 1 output line, it'll still be an array.
To know wether or not your command executed successfuly, you'll have to check your $ret variable's value. If it's anything other than zero, whatever you executed didn't run smoothly. A clean exit is marked by the program returning 0.
I can't, though, for the life of me understand why you'd bother executing a query like this. If you have mysql running, and you have php installed, why not use mysqli_* or PDO? it'll save you a lot of hasstle, and it'll improve both performance and safety of your script.
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.
I am trying to use the PHP exec() function.
If the return_var argument is present along with the output argument,
then the return status of the executed command will be written to this
variable.
If the execution was successful, it's 0. However, if there is an error, it can be a multitude of other integers. I can't seem to find anywhere what those integers correspond to. How should I interpret the integer that I get?
Update:
I really should have specified this originally, but I am executing another PHP script. Unlike rsync, which has exit values on its man page, I can't find an equivalent for PHP.
So what I am doing is something like:
$rv = exec('php file.php', $out, $rv);
The return value is dependent on the process/program that you ran with exec. For instance, if you ran grep:
The exit status is 0 if selected lines are found, and 1 if not
found. If an error occurred the exit status is 2. (Note: POSIX
error handling code should check for '2' or greater.)
rsync has about 20 different error exit codes, all painstakingly explained in the man page:
http://linux.die.net/man/1/rsync
so yes, it's program-dependant :)
Even if you're running PHP script, the exit value depends on your program itself. By default php scripts will exit with 0. If you use the exit function you can return different exit codes:
http://php.net/manual/en/function.exit.php
If you want to experimentally determine what your php program exits, call it on the command line:
php file.php
then do
echo $?
this will show you the exit value of your php script.
IMHO, before use exec() function better set output and return_var parameters and read return code execution by return_var.
Don't rely on exec() return value.
Look up the manual page for the command that you are executing. This value has nothing to do with PHP but the actual command.
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. ;)