Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-09-2012, 02:54 AM   PM User | #1
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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..
LearningCoder is offline   Reply With Quote
Old 12-09-2012, 05:16 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
LearningCoder (12-09-2012)
Old 12-09-2012, 11:21 AM   PM User | #3
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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..
LearningCoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:09 AM.


Advertisement
Log in to turn off these ads.