Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    912
    Thanks
    76
    Thanked 28 Times in 28 Posts

    Dealing with php functions which return multiple false types.

    Hi,

    I am just practicing some php and decided to attempt to search through my wamp directory as I've never really practiced this at all.

    Basically, I am using array_search() to search my directory files array for a value. The documentation states that if it doesn't match the value, it returns boolean false but can also return another type which value equates to false for example int type with a value of 0.

    My question is, what is the best way to deal with this? I'm kinda lost. I'm using array_search() because I need the index so I can build the path string up. I realize I could count the array length and loop, saving the value into a variable (if it is found) and then create my own variable and value should it not find the value.

    I'm wondering what the best way is to go about getting the values I need, and also how do we deal with these php functions which possibly return different types when it means false.(as in what do we check, boolean false, integer 0, string false) I will use mkdir() if it is false but I just need to know HOW to check the value effectively and minimizing the bugs.

    On php.net is states to check it using the identical operator but what if it does by chance return integer 0, it won't deal with the error properly. Maybe I could do a switch() but I think it could possibly be unnecessary?

    I've done a little bit of code:
    PHP Code:
    <?php

    $dir_name 
    dirname(__FILE__);
    echo 
    $dir_name."<br />";

    $dir_files scandir($dir_name);
    array_shift($dir_files);
    array_shift($dir_files);

    $index array_search("GardenableImproved",$dir_files);

    if(!
    $index)
    {
        
    }






    /*
    if($index){
        
        $site_dir = scandir($dir_name."\\".$dir_files[$index]);
        array_shift($site_dir);
        array_shift($site_dir);
    }
    else
    {
        echo "directory does not exist.";
    }

    */

    ?>
    Not sure if it helps much.

    Thank you for any information and help you can put my way.

    Kind regards,

    LC.
    Last edited by LearningCoder; 12-09-2012 at 03:03 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Use the identical operators === and !== to compare it to a specific datatype. In this case, using !== false and === false would tell you if it failed.
    Other commonly used functions that need identical checks are strtotime (which is a best between versions making its return type unstable), and strpos.

    BTW, if you are using a specific search for filesystems you can use a FilterIterator or a glob[iterator] as well.

  • Users who have thanked Fou-Lu for this post:

    LearningCoder (12-09-2012)

  • #3
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    912
    Thanks
    76
    Thanked 28 Times in 28 Posts
    Thank you for your reply.

    I'll have a good play about with it tonight when I get a chance.

    Kind regards,

    LC.
    Last edited by LearningCoder; 12-09-2012 at 09:23 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •