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

  • SEARCH
  • Home
  • 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 64135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:44:32+00:00 2026-05-10T18:44:32+00:00

Using PHP, what’s the fastest way to convert a string like this: 123 to

  • 0

Using PHP, what’s the fastest way to convert a string like this: '123' to an integer?

Why is that particular method the fastest? What happens if it gets unexpected input, such as 'hello' or an array?

  • 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. 2026-05-10T18:44:33+00:00Added an answer on May 10, 2026 at 6:44 pm

    I’ve just set up a quick benchmarking exercise:

    Function             time to run 1 million iterations -------------------------------------------- (int) '123':                0.55029 intval('123'):              1.0115  (183%)  (int) '0':                  0.42461 intval('0'):                0.95683 (225%)  (int) int:                  0.1502 intval(int):                0.65716 (438%)  (int) array('a', 'b'):      0.91264 intval(array('a', 'b')):    1.47681 (162%)  (int) 'hello':              0.42208 intval('hello'):            0.93678 (222%) 

    On average, calling intval() is two and a half times slower, and the difference is the greatest if your input already is an integer.

    I’d be interested to know why though.


    Update: I’ve run the tests again, this time with coercion (0 + $var)

    | INPUT ($x)      |  (int) $x  |intval($x) |  0 + $x   | |-----------------|------------|-----------|-----------| | '123'           |   0.51541  |  0.96924  |  0.33828  | | '0'             |   0.42723  |  0.97418  |  0.31353  | | 123             |   0.15011  |  0.61690  |  0.15452  | | array('a', 'b') |   0.8893   |  1.45109  |  err!     | | 'hello'         |   0.42618  |  0.88803  |  0.1691   | |-----------------|------------|-----------|-----------| 

    Addendum: I’ve just come across a slightly unexpected behaviour which you should be aware of when choosing one of these methods:

    $x = '11'; (int) $x;      // int(11) intval($x);    // int(11) $x + 0;        // int(11)  $x = '0x11'; (int) $x;      // int(0) intval($x);    // int(0) $x + 0;        // int(17) !  $x = '011'; (int) $x;      // int(11) intval($x);    // int(11) $x + 0;        // int(11) (not 9) 

    Tested using PHP 5.3.1

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

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If it's only returning one row, chances are there is… May 11, 2026 at 3:29 pm
  • added an answer Okay, rule of thumb: if you see a switch statement… May 11, 2026 at 3:29 pm
  • added an answer Give the button a x:name and you can see that… May 11, 2026 at 3:29 pm

Related Questions

Using PHP, what's the fastest way to convert a string like this: 123 to
Using PHP and MySQL, I have a forum system I'm trying to build. What
I'm using PHP and libtidy to attempt to screen scrape what might possibly be
I am wondering what the best way is using php to obtain a list
What is the best way to password protect folder using php without a database

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.