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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:33:46+00:00 2026-06-17T23:33:46+00:00

Possible Duplicate: PHP type-hinting to primitive values? Say, I create a function called retrieveCount($count){

  • 0

Possible Duplicate:
PHP type-hinting to primitive values?

Say, I create a function called

retrieveCount($count){
   return ++$count;
}

Just a simple example. How do I set the function to accept the type to be integers only?
in Java, or other languages, we have:

public int retrieveCount(int count){
   return ++count;
}

Is there a way do to the same thing in PHP? I read on the documentation that for OOP, PHP has type hinting for complex structures, such as objects, arrays, interfaces, etc, but no for scalar types (int, string).

Is this really the case, and we cannot specify the type?

Thank you

  • 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-17T23:33:47+00:00Added an answer on June 17, 2026 at 11:33 pm

    There are no direct scalar type hint possibilities PHP. But you can emulate it by checking the type inside the function and trigger an appropriate error:

    function retrieveCount( $count )
    {
        if( !is_int( $count ) )
        {
            // I believe E_USER_WARNING is the appropriate error level
            // equivalent to what PHP issues itself on type hint errors
            trigger_error(
                'Argument 1 passed to retrieveCount() must be an integer',
                E_USER_WARNING
            );
        }
    
        return ++$count;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP detecting request type (GET, POST, PUT or DELETE) This should be
Possible Duplicate: PHP create a file without fopen I want to create a file
Possible Duplicate: PHP function to strip tags, except a list of whitelisted tags and
Possible Duplicate: PHP foreach() return only last 50 items I need only the last
Possible Duplicate: Linkify Regex Function PHP Daring Fireball Method I'm trying to get URLs
Possible Duplicate: handle json request in PHP The following: $.ajax({ type: 'POST', url: 'receive-json.php',
Possible Duplicate: php $_POST array empty upon form submission I just make a simple
Possible Duplicate: uploaded file type check by PHP I used the below script for
I know you guys going to say this might be possible duplicate to PHP
Possible Duplicate: PHP detecting request type (GET, POST, PUT or DELETE) I want to

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.