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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:00:50+00:00 2026-06-08T09:00:50+00:00

I have situation where I have a variable being used in some arithmetic (mostly

  • 0

I have situation where I have a variable being used in some arithmetic (mostly just multiplication/division) in a PHP function. The variable is actually passed to the function from another. What gets passed, though, may be either a numerical value, or a string value.

So basically I have:

 $inverse = 1/$number;

Where $number may be either a valid number, or a string.

My question is what happens when $number is a string? How does php handle that situation?

Ultimately the answer isn’t too important; I can think of a couple of different ways of working around the situation. It’s just that it came up, I got curious, and a quick google search couldn’t answer my question. Even if I don’t need to know the answer, I’d like to know the answer, because understanding how PHP works will help me in the future.

Thanks.

  • 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-08T09:00:52+00:00Added an answer on June 8, 2026 at 9:00 am

    PHP will perform type-coercion (also known as type-juggling) and attempt to interpret the value as a number when it is a string when the variable is used in a numerical context.

    See the docs on the division operator:

    The division operator (“/”) returns a float value unless the two
    operands are integers (or strings that get converted to integers) and
    the numbers are evenly divisible, in which case an integer value will
    be returned.

    Also relevant, String conversions to a number:

    When a string is evaluated in a numeric context, the resulting value
    and type are determined as follows.

    ….

    The value is given by the initial portion of the string. If the string
    starts with valid numeric data, this will be the value used.
    Otherwise, the value will be 0 (zero). Valid numeric data is an
    optional sign, followed by one or more digits (optionally containing a
    decimal point), followed by an optional exponent. The exponent is an
    ‘e’ or ‘E’ followed by one or more digits.

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

Sidebar

Related Questions

The situation: I have multiple classes that should each hold a variable with a
I have a situation where I need to get the value of a variable,
I have a situation where thread 1 is waiting on a condition variable A,
I have a problematic situation with some quite advanced unit tests (using PowerMock for
So I have the following situation; I want to send a couple of variables
i have situation like this: class IData { virtual void get() = 0; virtual
I have situation, where running a query that filters by an indexed column in
I have situation in which I read a record from a database. And if
I have situation where I need to change the order of the columns/adding new
I have situation where a user can manipulate a large set of data (presented

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.