SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Handling errors directly in IF statement

    I've got a script that is moving through the FTP process, and testing multiple nested conditions. What I'd like to do is handle the error directly inside the IF statement, provided it's possible. Similar to the " or die()".

    Pseudo-example:

    if($conn = (ftp_connect OR $error = "failed to connect"))
    {
    if(ftp_login OR $error = "failed login")
    {
    if(ftp_put OR $error = "failed transfer"){}
    }
    }

    If something like this is possible, it'd save a lot of lines of code. Thanks guys.

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    do you get paid extra for using less lines of code? If not, why do you want to save lines at the cost of readability?

  3. #3
    SitePoint Addict Shaydez's Avatar
    Join Date
    Jul 2006
    Location
    Boca Raton, Florida
    Posts
    356
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    could try catch ?

    oh... i see what you're trying to do... best thing to do is Case Switch method. it'll clean up the code. but really depends what you want to do with each error.
    Sr. Website Developer and Internet Marketing
    www.CarlosJa.com Note: If anyone
    needs to get ahold of me please feel free to email me through
    my site. Apparently i missed quite a few private messages.

  4. #4
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nested conditions/loops is generally bad idea.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by boatmark View Post
    nested conditions/loops is generally bad idea.
    This is what I ended up doing:

    Code PHP:
    if(isset($_REQUEST["grab"]) && isset($_REQUEST["drop"]) && isset($_REQUEST["file_ref"]))
    {
    	$outcome = "";
    	$steps = array();
     
    	//conditionals for stepping through the ftp process. Retry values are for the subsequent conditional.
    	$retry = false;
    	if($steps["Maximum retry attempts reached."] = ($retry_attempt < $max_retrys))
    	{
    		$retry = true;
    		if($steps["FTP connection to host server/port failed."] = @ftp_connect($ftp["host"], $ftp["port"]))
    		{
    			$retry = false;
    			if(current($steps) && $steps["FTP login unsuccessful."] = @ftp_login(current($steps), $ftp["user"], $ftp["password"]))
    			{
    				$retry = false;
    				if(current($steps) &&  $steps["File not found on local server for upload."] = @filesize(urldecode($_REQUEST["grab"])))
    				{
    					$retry = true;
    					if(current($steps) && $steps["File upload failed during transfer."] = @ftp_put($steps["FTP Host Connect"],urldecode($_REQUEST["drop"]),urldecode($_REQUEST["grab"]), FTP_BINARY))
    					{
    						$filesize = array();
    						$remote_size = get_remote_size($ftp["depository_url"] . urldecode($_REQUEST["file_ref"]));
    						$filesize["remote"] = $remote_size["size"];
    						$filesize["local"] = filesize(urldecode($_REQUEST["grab"]));
     
    						$retry = true;
    						if(current($steps) && $steps["Local and remote file sizes do not match. File not verified."] = ($filesize["remote"] == $filesize["local"]))
    						{
    							$outcome = "<span class='success'>" . urldecode($_REQUEST["file_ref"]) . "</span> uploaded successfully. [<img src='checkbox.gif' alt='excellent! file verified'>] File Verified (<span class='filesize' style='display: none'>" . $filesize["local"] . "</span>" . number_format($filesize["remote"]/1024/1024, 2) . "mb)"
    						}
    					}
    				}
    			}
    		}
    	}
     
    	foreach($steps as $key => $val) //loop through steps, see if there was a false - failed - conditional. If so, also add retry if retry checks out true.
    	{
    		if(!$val)
    		{
    			$outcome = !$val?"<span class='failed_file'>" . urldecode($_REQUEST["file_ref"] . "</span>: " . $key . "." . (!$retry?"":" <span class='retrying'>Retrying</span> in <span class='retry_time'>" . $retry_interval . "</span>s.":"");
    		}
    	}
     
    	echo $outcome;
    }

    So I use an array with the keys set as the error msg, and if a key is set to false, that's the step the process seemingly broke on. Depending on the given error, the process could retry, so there's a retry variable set as well. If I could set this retry variable also in the if statement, I'd love it.

    I was playing around w/doing this in a catch statement, and somehow moving the pointer through the array as the conditions proved true, and see where I could potentially do this:

    switch(true)
    case (set array key to condition)
    set retry
    break;

    case (set array key to condition)
    set retry
    break;

    case (set array key to condition)
    set retry
    break;

    default


    The problem with this though is that since these are steps, I want to progress past the step if it proves truth (meaning no "break"), but if it's false, I want it to break and not try to test all the rest of the conditions. A nested loop would do that. One of these processes being an FTP login, the PHP actually gives a waiting/timeout period for the login attempt, which is a resource-heavy conditional, so I don't want to go through all conditionals to test true/false.

    Feedback appreciated.


Bookmarks

Posting Permissions

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