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
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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.