Re: Str to Int
| From: | tg-php at gryffyndevelopment dot com | Date: | Sat, 21 Jan 2006 00:12:45 +0000 |
| Subject: | Re: Str to Int | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-228996@lists.php.net to get a copy of this message | ||
Ok, some good points where made already, but let's pull it all together:
1. You should get in the habit of using $_GET[] instead of $HTTP_GET_VARS.. minor thing but worth
noting (info at http://us2.php.net/reserved.variables)
2. case reserve:
This, I believe, will treate "reserve" as a constant. Change it to:
case "reserve":
to make it work properly.
3. if ($cardID = '' || is_int($cardID))
As someone mentioned, $cardID = '' makes an assignment, doesn't test for
true/false. Change to $cardID == ''. And this statement SHOULD come up "true"
as a whole because $cardID = '' should always be true (unless the assignment fails). So
regardless of is_int($cardID) succeeding or failing, $cardID = '' should be true so this
statement will always be true.
4. $cardID = intval($cardID.trim())
Mixing PHP and Javascript? (or another language?). Try:
$cardID = intval($cardID);
trim() probably isn't necessary unless your variable starts with letters.
intval("123 main street") should give you "123".
intval("main street suite 400") should give you "0" (zero)
5. intval() returns zero if it's a bad entry, so is_int(intval($anything)) should always be
true I believe. Just something to keep in mind in case $cardID zero is actually something you want
to use. In that case you don't want to use $cardID = intval($whatever) because you'll end
up with zero.
6. exit(0)
Again remnants from another language. exit() works in PHP but no need to return a zero return
value. exit() is fine.
7. echo "reserve" is going to output "reserve", not the value of a variable
try echo "$reserve";
Also.. it's probably not necessary, but I usually enclose my switch statements in braces just
to keep it clear what's part of the switch.
switch ($somevar) {
case "val1":
echo "Value one";
break;
case "val2":
echo "Value two";
break;
default:
echo "No valid value";
break;
}
Good luck Ron! You'll get ahold of this stuff soon enough. Looks like you have some
experience in other languages (and some of it will work fine in PHP.. it's very forgiving and
flexible, but some of it will need to be tweaked).
Let us know if you have further questions.
-TG
= = = Original message = = =
[php]
if ($HTTP_GET_VARS) // if some GET variables were passed on,...
switch ($HTTP_GET_VARS['action'])// recognize the get-var 'action'
case reserve: // it says, 'reserve PINs'
$cardID = $HTTP_GET_VARS['cardID']; // get cardID which was passed by GET as well
if ($cardID=''||is_int($cardID)) // unless,
$cardID = intval($cardID.trim());
echo "<b>ERROR, cardID='' or cardID is no
integer!</b><br>\n"; //print Error
exit(0); //and leave
echo "reserve<br>\n"; // Variables were okay, so go ahead with the reserving
/**************** [reserve SKU] ****************/
$inputdata = '<xs:request xmlns:xs=\'urn:pinXpressSchema\'
version=\'1.5\' langCode=\'en\'>'.$n.'
<DEALERinfo aspName="'.$aspName.'"
~ dealerName="'.$dealerName.'"
posName="'.$posName.'"
posPassword="'.$posPassword.'"
userName="'.$userName.'"
userPassword="'.$userPassword.'"/>
<reservePINs>
<getPIN cardID="'.intval(trim($cardID)).'"
quantity="1" />
</reservePINs>
</xs:request>';
/**************** [/reserve SKU] ***************/
break;
[/php]
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.