Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8022057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:09:49+00:00 2026-06-04T22:09:49+00:00

There are multiple ways of converting a string to an integer. $someString = substr($input,

  • 0

There are multiple ways of converting a string to an integer.

$someString = substr($input, strripos($input, "_") + 1);
$i = $someString + 0;
$i = (int) $someString;
$i = intval($someString);

$someString comes from use input, so that might have to be taken into consideration. Which option is “better” or has the most negative side-effects? (i.e. performance-wise, security-wise)

$input can look something like 1_abcd_1 or 1_abcd_2. That is quite fixed — except for the abcd which is made up in this example. This input comes from radio buttons, but a user can always alter their data if they want to. Thus, I have to account for malicious data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:09:50+00:00Added an answer on June 4, 2026 at 10:09 pm

    Since the input is being cast to a number, there’s no security issues. Performance differences are negligible. However, the first one may not produce an integer in certain cases.

    For example, let $input = '19.99';.

    $i = $someString + 0;    // $i = 19.99
    

    The other two cases are equivalent, but $i = (int) $someString; averages approximately 56% faster than $i = intval($someString);. Since both execute in a matter of milliseconds, performance differences are generally negligible unless they occur in a sizable loop.

    $i = (int) $someString;      // $i = 9
    $i = intval($someString);    // $i = 9
    

    On the matter of security: if you expect $input to always be in a specific form (e.g. #_xxxx_#), then you should verify that it is in that format. Expanding on Jack’s comment — if you’re using PHP >= 5.2.0, you can check the format of the input using a regular expression.

    <?php
    $input = '1_abcd_1';
    
    // Set the desired format
    $filter_options = array(
        'options' => array(
            // choose a regular expression that matches your format
            'regexp' => '/^[\d]_[a-z]{4}_[\d]$/'
        )
    );
    
    $format = filter_var($input, FILTER_VALIDATE_REGEXP, $filter_options);
    if ($format !== false) {
        // process input
        $someString = substr($format, strripos($format, "_") + 1);
        $i = (int) $someString;
        echo '$i = ', $i;
    } else {
        // Handle invalid format
        echo 'Malformed data. Potentially malicious';
    }
    ?>
    

    This will completely reject things like:

    0xfe
    bob
    j_xtyz_5
    1_wxyz_22
    21_abcd_1
    1_Abcd_2
    

    Naturally, you would adjust the regular expression to your needs.

    See also:

    • Dev Shed Tutorials: Checking regular expressions with the filter extension – PHP
    • PHP Manual: filter_var

    If you’re not using PHP >= 5.2.0, you can use preg_match.

    <?php
    $input = '1_abcd_2';
    
    $valid = preg_match('/^[\d]_[a-z]{4}_[\d]$/', $input);
    if ($valid) {
        // process input
        $someString = substr($input, strripos($input, "_") + 1);
        $i = (int) $someString;
        echo '$i = ', $i;
    } else {
        // Handle invalid format
        echo 'Malformed data. Potentially malicious';
    }
    ?>
    

    Validating the format of the input let’s you know immediately if the data has been tampered with. Then you can act accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are multiple ways to select a particular option from a using
In bash, there are multiple ways to direct input and output. For example, these
While there are multiple ways to reverse bit order in a byte, I'm curious
I know a great thing about perl is that there's multiple ways of doing
In digital imaging, when overlaying two visual layers there are multiple ways you can
I understand there are multiple ways to receive Domino based email on a iPhone,
There appears to be multiple ways to do this, but the examples I've tried
There are multiple ways to fill an NSTableView up with data through either bindings,
There are multiple ways to convert a list of IP address to a list
I heard in Microsoft SQL Server there are multiple ways to find worst stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.