Jump to content

Convert a string to an integer


nyi8083

Recommended Posts

Does this function exist in PHP? I have been searching for this function for a while or any mention of it but have come up with nothing.  I'm using the $_GET['whatever'] to pass variables via the URL, and I am just not sure if that variable which is going to be a number needs to be converted to an integer if i want to use it to access part of an array.

for example, could this hypothetically work? ($a is an array)

$desc = $a[$_GET['whatever']];

I am assuming it will not, but my knowledge of php is very limited
Link to comment
https://forums.phpfreaks.com/topic/16172-convert-a-string-to-an-integer/
Share on other sites

in my opinion, if you're expecting a number and you recieve a string, you should reject the data but since you asked, there are two things you can do:

1. settype()
[code=php:0]
settype($_GET['whatever'] , "integer");
[/code]

2. type cast
[code=php:0]
$_GET['whatever'] = (integer) $_GET['whatever'];
[/code]
PHP is a typeless language, for the most part.
[quote]
for example, could this hypothetically work? ($a is an array)

$desc = $a[$_GET['whatever']];
[/quote]
Yes, that works fine.

Also, remember:
[code]<?php
$str_three = '3';
$three = 3;
$six = $str_three + $three;
echo $six;
?>[/code]

For more information, read this section in the find manual... http://www.php.net/manual/en/language.types.string.php

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.