Объяснение:
Шаг 1: int fun(int);
Здесь мы объявляем прототип функции fun()
.
Шаг 2: int i = fun(10);
Переменная i объявляется как целочисленный тип, и результат fun(10)
будет сохранен в переменной i
.
Шаг 3: int fun(int i){ return (i++); }
Внутри fun()
мы возвращаем значение return(i++)
. Возвращает 10
. потому что i++
является оператором после инсерции.
Шаг 4: Затем управление возвращается к основной функции, и значение 10
присваивается переменной i
.
Шаг 5: printf("%d\n", --i);
Здесь --i
обозначает предварительное слияние. Следовательно, он печатает значение 9
.
You could do it like this.
try {
//throw exception if can't move the file
if (!move_uploaded_file( ... )) {
throw new Exception('Could not move file');
}
//do some more things with the file which may also throw an exception
//...
//ok if got here
echo "Upload Complete!";
} catch (Exception $e) {
die ('File did not upload: ' . $e->getMessage());
}
It is a bit pointless for the above example, but you should get the idea. Note that you can throw the exception(s) from anywhere (e.g. within a function/method that you call from withing the try{}) and they will propagate upwards.
Well, if you want to use exceptions, you could do something like:
function handleUpload() {
$move = move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/uploads/".$_FILES['file']['name']);
if (!$move) {
throw new Exception('File Didnt Upload');
}
}
try {
handleUpload();
echo "File Uploaded Successfully";
} catch(Exception $ex) {
die($ex->getMessage);
}
I know this may seem like bloat - but you can call the method from anywhere in the call stack, and catch the exception at any point.
Операторы try-catch
используются для обработки исключений. Я не верю, что функция move_uploaded_files
может вызывать и исключение, поэтому я думаю, что ваш код написан правильно. После звонка смотришь код возврата. Если оно ложно, обработка прекращается, в противном случае вы сообщаете об успехе.
Согласно аналогичному сообщению в PHPbug, только объектно-ориентированный код (объектно-ориентированный код) выдает исключения. Это будет означать, что такие функции, как move_uploaded_file, не будут генерировать свои собственные исключения, но некоторый другой код будет.