S
S
Senseich2018-03-25 13:02:18
PHP
Senseich, 2018-03-25 13:02:18

How does the OR operator work on the example of the fopen function?

Help me figure out one subtlety that haunts me)
The documentation says that the OR operator first assigns, and then the second operand is ignored:

// Константа false присваивается $f, а затем значение true игнорируется
// Действует как: (($f = false) or true)
$f = false or true;

1. Ok, the first question, why then the second operand at all, if it is ignored, you can just write $f = false ?
But okay, using the fopen() function as an example:
$write = fopen($path, 'w+') or die("не удалось создать файл");

Из документации: Возвращает указатель на файл в случае успешной работы, 
или FALSE в случае ошибки

It can be seen from the rule that if an error occurs, then it returns FALSE.
From the rule above ($f = false) or true it should be that:
($write =  false) or die("не удалось создать файл");

die("не удалось создать файл")should be ignored.
I faked an error, but the message "failed to create file" is still displayed, why?
5ab7707373a9a163064444.jpeg
This OR confused me! )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil Sapegin, 2018-03-25
@Senseich

If the expression, function result or assignment on the left side of the OR is boolean false, then the right side of the OR operator will be executed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question