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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:34:39+00:00 2026-06-13T08:34:39+00:00

I was reading PHP manual and I came across type juggling I was confused,

  • 0

I was reading PHP manual and I came across type juggling

I was confused, because I’ve never came across such thing.

$foo = 5 + "10 Little Piggies"; // $foo is integer (15)

When I used this code it returns me 15, it adds up 10 + 5 and when I use is_int() it returns me true ie. 1 where I was expecting an error, it later referenced me to String conversion to numbers where I read If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero)

$foo = 1 + "bob3";             /* $foo is int though this doesn't add up 3+1 
                                  but as stated this adds 1+0 */

now what should I do if I want to treat 10 Little Piggies OR bob3 as a string and not as an int. Using settype() doesn’t work either. I want an error that I cannot add 5 to a string.

  • 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-13T08:34:40+00:00Added an answer on June 13, 2026 at 8:34 am

    If you want an error, you need to trigger an error:

    $string = "bob3";
    if (is_string($string)) 
    {
        trigger_error('Does not work on a string.');
    }
    $foo = 1 + $string;
    

    Or if you like to have some interface:

    class IntegerAddition
    {
        private $a, $b;
        public function __construct($a, $b) {
            if (!is_int($a)) throw new InvalidArgumentException('$a needs to be integer');
            if (!is_int($b)) throw new InvalidArgumentException('$b needs to be integer');
            $this->a = $a; $this->b = $b;
        }
        public function calculate() {
            return $this->a + $this->b;
        }
    }
    
    $add = new IntegerAddition(1, 'bob3');
    echo $add->calculate();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

while reading abt array_filter() from php manual,came to face example to demostrate the same
I was reading php functions and I came across symlink but I really couldnt
As i am reading manual of php for array functions, i see that some
I'm reading the manual here: http://zendframework.com/manual/en/zend.view.helpers.html but I'm still confused. I have a script
I was reading about PDO and I came across the parse_ini_file function. A number
Was reading the PHP manual on integer overflow in the manual and was wondering:
I am looking into building a login system and after reading the php manual
while reading about declare construct from php manual i tried the following example using
I was reading the PHP manual for mysqli_stmt_bind_result and saw this code in the
breaks at my while loop. have been reading the php manual but can't figure

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.