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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:34:32+00:00 2026-05-14T02:34:32+00:00

I am executing this statement under while (($data=fgetcsv($this->fin,5000,;))!==FALSE) Now what I want in else

  • 0

I am executing this statement under while (($data=fgetcsv($this->fin,5000,";"))!==FALSE)

Now what I want in else loop is to throw exception only for data value which did not satisfy the if condition. Right now am displaying the complete row as I am not sure how to throw exception only for data which does not satisfy the value.

Code

if ((strtotime($data[11]) &&strtotime($data[12])&&strtotime($data[16]))!==FALSE 
&& ctype_digit($data[0]) && ctype_alnum($data[1]) && ctype_digit($data[2]) 
&& ctype_alnum($data[3]) && ctype_alnum($data[4]) && ctype_alnum($data[5]) 
&& ctype_alnum($data[6]) && ctype_alnum($data[7]) && ctype_alnum($data[8]) 
&& $this->_is_valid($data[9]) && ctype_digit($data[10]) && ctype_digit($data[13]) 
&& $this->_is_valid($data[14]))
{
     //Some Logic
}
else
{
    throw new Exception ("Data {$data[0], $data[1], $data[2], $data[3],
    $data[4], $data[5], $data[6], $data[7],
    $data[8], $data[9], $data[10], $data[11], $data[12],
    $data[13], $data[14], $data[16]} is not in valid format");
}

Guidance would be highly appreciated as to how can I throw exception only for data which did not satisfy the if value.

  • 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-14T02:34:32+00:00Added an answer on May 14, 2026 at 2:34 am

    Why not separate the tests ? i.e. do each test one by one, and throw an exception if a specific test fails ?

    Copy-pasting from your code, it would probably look like this :

    if (!strtotime($data[11])) {
        throw new Exception("field 11 : {$data[11]} is not a valid date");
    }
    if (!strtotime($data[12])) {
        throw new Exception("field 12 : {$data[12]} is not a valid date");
    }
    // Some more...
    if (!ctype_alnum($data[8])) {
        throw new Exception("field 8 : {$data[8]} is not a valid alnum");
    }
    // And so on...
    
    // And when all is tested, you know the items 
    // in $data are all OK
    

    This way :

    • You can know which field caused a validation failure
    • If you have several distinct tests on the same field, you can know which specific test failed

    And, as a possibility, you could (if needed) thrown different kind of exceptions, depending on the test that failed (i.e. one kind of exception for dates, one for integers, …) — in some cases, that might be useful.


    Edit after the comment : more full example

    Yes, you can validate field by field, and still work line by line.

    You just have to wrap your testing code in a try/catch block, that’s inside the loop that goes line by line ; a bit like that :

    $validData = array();
    $errors = array();
    
    while ($data = fgetcsv($f)) {
        try {
            // Validate the data of the current line
            // And, if valid, insert it into the database
    
            if (!strtotime($data[11])) {
                throw new Exception("field 11 : {$data[11]} is not a valid date");
            }
            if (!strtotime($data[12])) {
                throw new Exception("field 12 : {$data[12]} is not a valid date");
            }
            // Some more...
            if (!ctype_alnum($data[8])) {
                throw new Exception("field 8 : {$data[8]} is not a valid alnum");
            }
            // And so on...
    
            // And when all is tested, you know the items 
            // in $data are all OK
            // => which means it can be inserted into the DB
            // Or you can just put the valid data into a "temporary" array, that will be used later :
            $validData[] = $data;
    
        } catch (Exception $e) {
            // An error has occurend on the current line
            // You can log it, if necessary :
            $errors[] = $e->getMessage();
        }
    }
    
    // Now that the whole file has been read, test if there's been an error :
    if (empty($errors)) {
        // No error
        // => insert the data that's in $validData
    } else {
        // There's been an error
        // => don't insert the data that's in $validData, if you don't want to insert anything
    }
    

    With that :

    • If there is an exception on one line (i.e. validation fails), you’ll jump to the catch block, to deal with the problem
    • And the loop will then restart, for the next line.

    EDIT: (By Yacoby)
    Rather than having endless if statments, you could just define which elements should be checked by which function and then process them in a loop. That way it avoids having 16 if statements.

    Code example:

    foreach ( array(11,12,16) as $index ){
        if ( !strtotime($data[$i]) ){
            throw new Exception("field {$i} : {$data[$i]} is not a date");
        }
    }
    
    foreach ( array(1,3,4,5,6,7,8) as $index ){
        if ( !ctype_alnum($data[$i]) ){
            throw new Exception("field {$i} : {$data[$i]} is not alphanumeric");
        }
    }
    
    foreach ( array(2, 10) as $i ){
        if ( !ctype_digit($data[$i]) ){
            throw new Exception("field {$i} : {$data[$i]} is not a valid digit");
        }
    }
    
    if ( !$this->_is_valid($data[14]) ){
        throw new Exception("field {14} : {$data[14]} is not valid");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 373k
  • Answers 373k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Putting the control that triggers the asyncpostback, inside the update… May 14, 2026 at 7:37 pm
  • Editorial Team
    Editorial Team added an answer Do not worry about malicious plugins. If somebody managed to… May 14, 2026 at 7:37 pm
  • Editorial Team
    Editorial Team added an answer Sure: SELECT name, create_date, modify_date FROM sys.tables SELECT name, create_date,… May 14, 2026 at 7:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.