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 3660650
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:13:38+00:00 2026-05-19T01:13:38+00:00

Throughout much of the code I review, I often see things such as: //

  • 0

Throughout much of the code I review, I often see things such as:

// $myvar could be anything

if (intval($myvar) > 0) {
  // do stuff
}

Or similarly:

if ($myvar != null && intval($myvar) > 0) {
  // do stuff
}

In an ongoing quest to produce more elegant and less-wasteful code, I am wondering if it is necessary to call intval(), for example, when performing a numeric comparison. My understanding from reading the documentation and from various responses here is that when a numeric comparison is asked for, it checks the type of the var and then performs either an integer cast or a string conversion depending on what it finds — or are these the same as far as PHP is concerned, operations-wise?

Based on that, it seems the least expensive way is to tell PHP to cast it directly as an integer, as such:

if ((int)$myvar > 0) {
  // do stuff
}

However, I almost never see it written that way. Am I missing anything? Does PHP automatically do this any time it seems a comparison operator?

I realize in 99.9% of situations it is sufficient to just perform the comparison and let PHP type juggle on its own, but I see this so often that either I am missing something or many others are!

  • 1 1 Answer
  • 2 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-05-19T01:13:38+00:00Added an answer on May 19, 2026 at 1:13 am

    You haven’t given a reason to cast at all:

    $myvar > 0
    

    will have the same effect. Either way, non-numeric strings can be coerced to 0 (this is one of PHP’s many strange design choices), and numeric strings are coerced to the number they represent. Thus after:

    $myvar = "foo";
    

    the following are identical (and false):

    (int) $myvar > 0
    $myvar > 0
    

    Of course, casting to int may be useful in other situations. Another useful function is is_numeric, which checks for a number or numeric string.

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

Sidebar

Related Questions

I'm still learning ASP.NET and I often see code like this throughout parts of
I have been using code similar to this MessageDlg('', mtWarning, [mbOK], 0); throughout my
Throughout a Bison grammar I am using right recursion, and I have read that
Throughout my app, I'm getting semantic issue warnings when I set ViewController.delegate = self
Throughout this day I've been investing time into fingerprint matching/recognition algorithms/implementations in the world
All throughout an application wherever error messages (or other user messages) are used I
On various pages throughout my PHP web site and in various nested directories I
In the UK throughout the 80's and 90's (70's too I believe!) there was
As discussed throughout the various MVC questions and blogposts , we know that the
Several times throughout the course of our current project, the visual studio designer has

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.