Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP die() vs. echo

Tags:

php

Can anyone please tell me why line 1 works (returns 35434), but line 2 doesn't (returns blank)?

  1. echo $_FILES['userfile']['size'];
  2. die ($_FILES['userfile']['size']);

Thanks!

like image 391
Michael Avatar asked Apr 29 '11 00:04

Michael


2 Answers

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']);
like image 113
Mark Elliot Avatar answered Oct 12 '22 05:10

Mark Elliot


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']);
like image 29
Alix Axel Avatar answered Oct 12 '22 05:10

Alix Axel