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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:19:01+00:00 2026-06-18T03:19:01+00:00

I don’t have enough experience with PHP and programming at all, so I want

  • 0

I don’t have enough experience with PHP and programming at all, so I want to know, what the best decision in my situation is.
Here is my code sample:

public static function isAscii($string, $type, $length = -1)
    {
        if (!is_string($string))
            throw new InternalException(ExceptionMessage::invalidFunctionParameterType(__CLASS__, __METHOD__));

        if ($length !== -1 && !Str::isValidLength($string, $length))
            return FALSE;

        $ascii_array = Constants::asciiRegex();

        if (!preg_match($ascii_array[$type], $string))
            return FALSE;

        return TRUE;
    }

I want to ensure that key $type exists for array $ascii_array. What is the best way to do that?

I can offer two decisions:

  1. The simplest. Just check key existence manually with array_key_exists() function. If key doesn’t exist – throw an exception, also manually. But I think that is not a good idea and checking this condition is redundant, because PHP compiler will generate warning if I don’t write anything (Notice: Undefined index:) and I want to use this somehow. I found there are some predefined exceptions like OutOfBoundsException and OutOfRangeException, it would be easy just to use them like in Java, but it seems that PHP doesn’t throw these exceptions, only warnings and errors.
  2. Use set_error_handler() function to ‘convert’ errors and warnings to exceptions. But there is another problem – how can I handle all possible errors? I tried to find the complete list of PHP errors and warnings, but it doesn’t exist.
    So I have no idea how to solve this problem, please help me.
    Thanks in advance, hope you will understand my question even with my poor English level.

P.S. this is an array with regular expressions for different types of ascii strings.enter image description here

  • 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-18T03:19:02+00:00Added an answer on June 18, 2026 at 3:19 am

    I hope I’m understanding this question correctly –
    Are you basically asking if there is a natural internal exception to adopt instead of running your own exception?

    Or are you just not aware you can do something like:

    try
    {
        //some code to try
        if (!in_array($type, $ascii_array)) {
            throw new Exception('type not in ascii array');
        }
    }
    catch(Exception $e) {
        die ('my custom exception message: ' . $e->getMessage());
    }
    

    if i missed the point, let me know and I may update.

    As for a reason to not throw your own exceptions… I can’t say I know of a real benefit to this. It would seem to me that in_array() is perfectly acceptable and throwing exceptions may just be personal preference and not necessarily a worthwhile concern for performance.

    EDIT

    Considering my answer and the question, it is noteworthy that this answer was accepted likely due to the conclusion in the comments that PHP does not allow you to declare functions that throw exceptions like Java does. Simply put.

    So this answer is to serve only that purpose. Can PHP declare a function that throws an exception? No, not by default. You have to try and catch the exceptions yourself within the function.

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

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
Don't know if you got my question, so here is an example. I have
Don't know if this is possible, but I have some code like this: val
Don't ask me how but I'm in a situation where I have DCPs published
I don't know why, but this code worked for me a month ago... maybe
Don't know if I'm using the not selector incorrectly but I want everything clicked
Don't they both have to convert to machine code at some point to execute
Don't know what's wrong here, when I run the application it says Specified method
Don't know if this has been answered before. Have custom routes to users. If
Don't know why I can't reply to people on here with only a small

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.