...

View Full Version : What is wrong with this code



Dylan Leblanc
03-20-2003, 12:54 PM
When this code is run the output is not what I expect.


<?

echo ( $id = (int) $_GET['id'] ? "[id{$id}]" : '' ) . ', ';
echo ( $id = (int) $_GET['id'] ? "?id={$id}" : '' );

?>


The output I want is:

[id12], ?id=12


But the output I get is:

[id12], ?id=[id12]


Can anyone tell what is wrong with the code? Is there something incorrect about the order of operations in the conditional expressions?

firepages
03-20-2003, 01:10 PM
well



<?
echo ( $id = (int) $_GET['id'] ? "[id{$_GET['id']}]" : '' ) . ', ';
echo ( $id = (int) $_GET['id'] ? "?id={$_GET['id']}" : '' );
?>


gives the answer you are looking for ... as does



<?
echo ((int) $_GET['id'] ? "[id{$id}]" : '' ) . ', ';
echo ((int) $_GET['id'] ? "?id={$id}" : '' );
?>


lol actually with register_globals=off the second one does not


but not having a clue what you are doing I dont know if that helps :confused:

V@no.
03-20-2003, 01:57 PM
what is (int) for? are u trying check if $id is a number not?
did u mean intval?
if so, probably this how it should be?
<?
echo ( ($id = intval($_GET['id'])) ? "[id{$_GET['id']}]" : '' ) . ', ';
echo ( ($id = intval($_GET['id'])) ? "?id={$_GET['id']}" : '' );
?>

Dylan Leblanc
03-20-2003, 02:04 PM
(int) reduces the value to an integer, so if the value was a string, the value would become 0, thus making the conditional expression false, and nothing would happen. It is a way of validating the incoming data.

Firepages, yes I can get some of your suggestions to work, kind of a combination of the two. Thanks. :)

Spookster
03-20-2003, 04:47 PM
Originally posted by Dylan Leblanc
(int) reduces the value to an integer

The proper terminology is "typecasting" for us programmer geeks. :)

V@no.
03-20-2003, 05:22 PM
Originally posted by Spookster
The proper terminology is "typecasting" for us programmer geeks. :)
hmmm...and why it's not published as a function at www.php.net ?
or it's not actualy function? :confused:

firepages
03-20-2003, 05:23 PM
geek ! - speak for yourself Spooks :D - lol & I just been told I am a scripter not a programmer :( the shame ..... anyway

ok, also look at the '===' operator , it checks for type as well...



<?
$wha='1';
//$wha=1;
echo ((int)$wha=== $wha)?'true':'false';
?>

firepages
03-20-2003, 05:27 PM
V@no , I dont think its technically a function as such , ? I dunno - anyway its just like typecasting in C which PHP is made from so it all makes sense eventually ~ check out
http://www.php.net/manual/en/language.types.type-juggling.php

Spookster
03-20-2003, 06:16 PM
Originally posted by firepages
geek ! - speak for yourself Spooks :D - lol & I just been told I am a scripter not a programmer :( the shame ..... anyway

ok, also look at the '===' operator , it checks for type as well...



<?
$wha='1';
//$wha=1;
echo ((int)$wha=== $wha)?'true':'false';
?>


you are a GID. :D

firepages
03-21-2003, 06:20 PM
GID ....

scared to ask :)

Spookster
03-21-2003, 06:47 PM
Originally posted by firepages
GID ....

scared to ask :)

I was waiting for you to ask. lol

GID(Geek in Denial) :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum