Jump to content

why ++ does not work?


Bogey

Recommended Posts

Why doesn't this code work?

$number=$row['number'];$number++;die($number);

This works!:

$number=$row['number'];die($number);

The $row['number'] is coming from a database (Type:

int(5))

) So the cell is declared a integer, so I dont get it, why it wont increase in PHP?!?!?! The second code gives a number.... the first code gives NOTHING! Any help? ;)

Link to comment
Share on other sites

All MySQL data is interpretted in PHP as a string, it's only a number for the database engine. You can cast it to integer or float with intval() or floatval().

$number = intval($row['number']);echo $number;

Link to comment
Share on other sites

All MySQL data is interpretted in PHP as a string, it's only a number for the database engine. You can cast it to integer or float with intval() or floatval().
PHP does automatic typecasting. You shouldn't have to manually convert anything. This:
$var1 = '5';$var2 = 5;echo $var1+$var2."<br />";echo ($var1++)."<br />";echo $var1."<br />";echo ($var2++)."<br />";echo $var2."<br />";

produces:

105656

Link to comment
Share on other sites

The problem is the use of die, which is the same as exit. It performs differently if you pass it a string vs an int.

If status is a string, this function prints the status just before exiting. 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.
If you want to output a value, use echo or print. You can use exit or die after that, but don't use die to output.
Link to comment
Share on other sites

Those aren't the same, exit and die are more in line with the C style of programs sending a status when they exit. In a C program, the main function is supposed to always return int, and it should return 0 if the program quit normally. e.g.:

#include <stdio.h> int main(void){  printf('Hello, world');  return 0;}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...