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.

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?

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.

nested conditions/loops is generally bad idea.

This is what I ended up doing:

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.