How to display 0 and true in "is_int" in php?

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2180 Views - Last Post: 02 February 2013 - 03:46 AM Rate Topic: -----

#1 brerallia  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 21-January 13

How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 02:30 AM

The instruction is to get the is_int with the following.

Example:
$num1 = 123;
$num2 = "123";

then get the is_int:
is_int ($num1); => result is true but displays in the browser 1 the instruction is to display true
is_int ($num2); => none is displayed in the browser since the answer to that condition is false, but the instruction is to display in the browser 0

how to get this?
any idea?

any help will be appreciated.. thank you!! :)

Is This A Good Question/Topic? 0
  • +

Replies To: How to display 0 and true in "is_int" in php?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 02:32 AM

how about an if() clause?

admittedly, not what Id use in the first place
Was This Post Helpful? 1
  • +
  • -

#3 brerallia  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 21-January 13

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 02:42 AM

View PostDormilich, on 31 January 2013 - 02:32 AM, said:

how about an if() clause?

admittedly, not what Id use in the first place


yes i do use if clause in true to output true but in false to get 0 is really hard...
this is what i did in my code to get the value 0 for false
<?php if (is_int($num2 == false)) echo 0; ?>
but still it doesnt print something... :(
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 03:20 AM

View Postbrerallia, on 31 January 2013 - 10:42 AM, said:

<?php if (is_int($num2 == false)) echo 0; ?>
but still it doesnt print something... :(/>

of course. the inner statement is always a boolean, which is obviously not an integer.
Was This Post Helpful? 1
  • +
  • -

#5 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 928
  • View blog
  • Posts: 3,212
  • Joined: 19-January 10

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 10:20 AM

Are you trying to output "true" if the number is a 1 and "false" if the number is a 0? using is_int is not the right way to do this.

$number = 0;
if($number === 1){
    echo "true";
} else {
    echo "false";
}



What this does is it checks to see if our $number is 1, and is an integer. If I recall correctly strings may be equal to one or something like that, so we don't want any error messages or weird data showing up as "true". If it doesn't equal 1, then we display false.

This can be shortened with a ternary statement like:
$number = ($number === 1) ? "true" : "false"; // $number is now "true" if equal to 1, and "false" if not.

Was This Post Helpful? 1
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,888
  • Joined: 08-August 08

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 10:43 AM

You could use an array:
<?php
$x = array(0.2, 1, 2, 3, 4.5, 6, "1", "2.3");
$T_F = array("False","True");
for($i = 0; $i < count($x); $i++) {
	echo $x[$i]." is an integer: ".$T_F[is_int($x[$i])]."<br>";
}

Note that is_int() will return false for strings so "1" shows as false.
Was This Post Helpful? 1
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,527
  • Joined: 12-December 12

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 10:56 AM

I think we've lost track of the original assignment, which was to use is_int:

$num1 = 123;
$num2 = "123";
echo is_int($num1) ? "true" : 0;
echo is_int($num2) ? "true" : 0;

This post has been edited by andrewsw: 31 January 2013 - 10:57 AM

Was This Post Helpful? 1
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,888
  • Joined: 08-August 08

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 10:59 AM

It's in there! Buried in line 5.
;)
Was This Post Helpful? 1
  • +
  • -

#9 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,527
  • Joined: 12-December 12

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 11:03 AM

View PostCTphpnwb, on 31 January 2013 - 10:59 AM, said:

It's in there! Buried in line 5.
;)/>

If you modified your code to the following it would be :)
$T_F = array(0, "True");

Was This Post Helpful? 2
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 12:34 PM

I would have gone with a simple
echo (int) is_int($num);

Was This Post Helpful? 2
  • +
  • -

#11 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,527
  • Joined: 12-December 12

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 12:44 PM

View PostDormilich, on 31 January 2013 - 12:34 PM, said:

I would have gone with a simple
echo (int) is_int($num);

This also doesn't meet the OPs requirement, which is to display "true" or 0.
Was This Post Helpful? 1
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 12:47 PM

aw, right. but the description in the OP is really quite contradictory, displaying "true" or "0" (though possible) does not make much sense (Id either let display "true"/"false" or "1"/"0").
Was This Post Helpful? 1
  • +
  • -

#13 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,527
  • Joined: 12-December 12

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 12:53 PM

View PostDormilich, on 31 January 2013 - 12:47 PM, said:

aw, right. but the description in the OP is really quite contradictory, displaying "true" or "0" (though possible) does not make much sense (Id either let display "true"/"false" or "1"/"0").

Well, yes, I agree it doesn't make sense :) but it is an assignment, and instructions for it.. So while the other answers here are instructional they are probably distracting for the OP.
Was This Post Helpful? 1
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 02:00 PM

another option worth checking is whether the assignment was copied correctly into the OP ...
Was This Post Helpful? 1
  • +
  • -

#15 brerallia  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 21-January 13

Re: How to display 0 and true in "is_int" in php?

Posted 31 January 2013 - 07:40 PM

View Postandrewsw, on 31 January 2013 - 10:56 AM, said:

I think we've lost track of the original assignment, which was to use is_int:

$num1 = 123;
$num2 = "123";
echo is_int($num1) ? "true" : 0;
echo is_int($num2) ? "true" : 0;



wow!!! your answer works!! but could you explain to me why there is a : and 0? what are the purpose of these things?? i really want to learn and thank you for helping me!! :)
im a beginner in php..
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2