Integer is not being shown as die() argument?
Asked Answered
F

3

10

I have this strange problem. When debugging, I have sometimetimes code looking like this

<?php
$var = 15;
die($var);

die() function works, but outputs nothing

However, this one works

<?php
$var = 15;
die($var."<-");

http://sandbox.phpcode.eu/g/81462.php

How is it possible? Did I miss something? or is it bug?

Fullerton answered 2/8, 2011 at 14:1 Comment(0)
B
12

See http://www.php.net/manual/en/function.exit.php (die() is equivalent to exit())

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.

Bingaman answered 2/8, 2011 at 14:3 Comment(3)
Yes, so I totally missed that. Thank you.Fullerton
i.e., use $var = '15' if you want it to print.Victualler
@Matthew: I never use '' for numbersFullerton
O
1

die() is the same as exit(), looking at the exit docs it takes 1 parameter, $status, the parameter information states

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.

Note: PHP >= 4.2.0 does NOT print the status if it is an integer.

Self-explanatory really, if you want to pass a number you need to type it to a string like so:

die( (string)$code );
Oberammergau answered 2/8, 2011 at 14:5 Comment(2)
I know it's horror to use "$code", but wouldn't it do the same?Fullerton
no, if the variable passes is an integer then it's process for internal exiting only, if it's a string regardless weather numeric it will process as outputOberammergau
I
0

die() function needs a string parameter.

In your second example

die($var."<-");

$var is converted into String before concat with "<-". So this line will print out "15<-". This is normal. There is neither a bug nor any thing wrong.

Ingratiate answered 12/6, 2012 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.