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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:28:34+00:00 2026-05-16T20:28:34+00:00

After some work in C and Java I’ve been more and more annoyed by

  • 0

After some work in C and Java I’ve been more and more annoyed by the wild west laws in PHP. What I really feel that PHP lacks is strict data types. The fact that string(‘0’) == (int)0 == (boolean)false is one example.

You cannot rely on what the data type a function returns is. You can neither force arguments of a function to be of a specific type, which might lead to a non strict compare resulting in something unexpected. Everything can be taken care of, but it still opens up for unexpected bugs.

Is it good or bad practice to typecast arguments received for a method? And is it good to typecast the return?

IE

public function doo($foo, $bar) {
   $foo = (int)$foo;
   $bar = (float)$bar;
   $result = $bar + $foo;
   return (array)$result;
}

The example is quite stupid and I haven’t tested it, but I think everyone gets the idea. Is there any reason for the PHP-god to convert data type as he wants, beside letting people that don’t know of data types use PHP?

  • 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-05-16T20:28:34+00:00Added an answer on May 16, 2026 at 8:28 pm

    For better or worse, loose-typing is “The PHP Way”. Many of the built-ins, and most of the language constructs, will operate on whatever types you give them — silently (and often dangerously) casting them behind the scenes to make things (sort of) fit together.

    Coming from a Java/C/C++ background myself, PHP’s loose-typing model has always been a source of frustration for me. But through the years I’ve found that, if I have to write PHP I can do a better job of it (i.e. cleaner, safer, more testable code) by embracing PHP’s “looseness”, rather than fighting it; and I end up a happier monkey because of it.

    Casting really is fundamental to my technique — and (IMHO) it’s the only way to consistently build clean, readable PHP code that handles mixed-type arguments in a well-understood, testable, deterministic way.

    The main point (which you clearly understand as well) is that, in PHP, you can not simply assume that an argument is the type you expect it to be. Doing so, can have serious consequences that you are not likely to catch until after your app has gone to production.

    To illustrate this point:

    <?php
    
    function displayRoomCount( $numBoys, $numGirls ) {
      // we'll assume both args are int
    
      // check boundary conditions
      if( ($numBoys < 0) || ($numGirls < 0) ) throw new Exception('argument out of range');
    
      // perform the specified logic
      $total = $numBoys + $numGirls;
      print( "{$total} people: {$numBoys} boys, and {$numGirls} girls \n" );
    }
    
    displayRoomCount(0, 0);   // (ok) prints: "0 people: 0 boys, and 0 girls" 
    
    displayRoomCount(-10, 20);  // (ok) throws an exception
    
    displayRoomCount("asdf", 10);  // (wrong!) prints: "10 people: asdf boys, and 10 girls"
    

    One approach to solving this is to restrict the types that the function can accept, throwing an exception when an invalid type is detected. Others have mentioned this approach already. It appeals well to my Java/C/C++ aesthetics, and I followed this approach in PHP for years and years. In short, there’s nothing wrong with it, but it does go against “The PHP Way”, and after a while, that starts to feel like swimming up-stream.

    As an alternative, casting provides a simple and clean way to ensure that the function behaves deterministically for all possible inputs, without having to write specific logic to handle each different type.

    Using casting, our example now becomes:

    <?php
    
    function displayRoomCount( $numBoys, $numGirls ) {
      // we cast to ensure that we have the types we expect
      $numBoys = (int)$numBoys;
      $numGirls = (int)$numGirls;
    
      // check boundary conditions
      if( ($numBoys < 0) || ($numGirls < 0) ) throw new Exception('argument out of range');
    
      // perform the specified logic
      $total = $numBoys + $numGirls;
      print( "{$total} people: {$numBoys} boys, and {$numGirls} girls \n" );
    }
    
    displayRoomCount("asdf", 10);  // (ok now!) prints: "10 people: 0 boys, and 10 girls"
    

    The function now behaves as expected. In fact, it’s easy to show that the function’s behavior is now well-defined for all possible inputs. This is because the the cast operation is well-defined for all possible inputs; the casts ensure that we’re always working with integers; and the rest of the function is written so as to be well-defined for all possible integers.

    Rules for type-casting in PHP are documented here, (see the type-specific links mid-way down the page – eg: “Converting to integer”).

    This approach has the added benefit that the function will now behave in a way that is consistent with other PHP built-ins, and language constructs. For example:

    // assume $db_row read from a database of some sort
    displayRoomCount( $db_row['boys'], $db_row['girls'] ); 
    

    will work just fine, despite the fact that $db_row['boys'] and $db_row['girls'] are actually strings that contain numeric values. This is consistent with the way that the average PHP developer (who does not know C, C++, or Java) will expect it to work.


    As for casting return values: there is very little point in doing so, unless you know that you have a potentially mixed-type variable, and you want to always ensure that the return value is a specific type. This is more often the case at intermediate points in the code, rather than at the point where you’re returning from a function.

    A practical example:

    <?php
    
    function getParam( $name, $idx=0 ) {
      $name = (string)$name;
      $idx = (int)$idx;
    
      if($name==='') return null;
      if($idx<0) $idx=0;
    
      // $_REQUEST[$name] could be null, or string, or array
      // this depends on the web request that came in.  Our use of
      // the array cast here, lets us write generic logic to deal with them all
      //
      $param = (array)$_REQUEST[$name];
    
      if( count($param) <= $idx) return null;
      return $param[$idx];
    }
    
    // here, the cast is used to ensure that we always get a string
    // even if "fullName" was missing from the request, the cast will convert
    // the returned NULL value into an empty string.
    $full_name = (string)getParam("fullName");
    

    You get the idea.


    There are a couple of gotcha’s to be aware of

    • PHP’s casting mechanism is not smart enough to optimize the “no-op” cast. So casting always causes a copy of the variable to be made. In most cases, this not a problem, but if you regularly use this approach, you should keep it in the back of your mind. Because of this, casting can cause unexpected issues with references and large arrays. See PHP Bug Report #50894 for more details.

    • In php, a whole number that is too large (or too small) to represent as an integer type, will automatically be represented as a float (or a double, if necessary). This means that the result of ($big_int + $big_int) can actually be a float, and if you cast it to an int the resulting number will be gibberish. So, if you’re building functions that need to operate on large whole numbers, you should keep this in mind, and probably consider some other approach.


    Sorry for the long post, but it’s a topic that I’ve considered in depth, and through the years, I’ve accumulated quite a bit of knowledge (and opinion) about it. By putting it out here, I hope someone will find it helpful.

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

Sidebar

Related Questions

I have my java-written application being killed after some time of work. Java application
That title may need some work. Perhaps after reading this, you may be able
After some experience with functional languages, I'm starting to use recursion more in Java
I'm new to PHP, mysql etc. I've done some work in VB and Java,
I've got a Java/Flex project that I'm building using Maven. After doing some research
After inheriting some Zend Framework code it didn't work, after lots of fiddling I've
I'm trying to understand how ID3 tags work, so, after reading some documentation, I
Does anybody know a Java implementation of the DRMAA-API that is known to work
I'm doing some socket work in Java, and am trying to encode the data
After some time of working with Eclipse - Java Enterprise, STS etc. I found

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.