PHP conditions (if): short write

Conditions occupy a significant part of the algorithms. PHP is a progressive programming language whose developers offer programmers the best forms for writing syntax. The ternary form of the if statement is convenient to use and makes the script code visual.

PHP if (in a short entry) received not so much recognition as it became the progenitor of the original conditional constructs. Many programmers consider it their duty to express their own style in the “manufacture” of original forms of expression of conditions.

The general form of a conditional statement

Conditions - The oldest syntactic construct. Along with the assignment operator - the operator of a simple action, the conditions have come a long way in the development of programming in general.

Choosing the right path

Before the younger brother of the switch / case operator appeared, a lot of water had leaked, programmers in various languages ​​composed bulky constructions from multi-line if statements.

By the time PHP came into being, programmers had gained experience in forming the right conditions. The idea of ​​PHP, a server language that by definition is destined to be compact and fast, quickly came to the optimal syntax and forms of expressing meaning, convenient for perception and understanding.

PHP if in short form and in full form is equally convenient and comfortable to perceive in the text of the script code.

If application example

In the example shown in the photo, (1) is the initial data, (2) is the full form of the conditional operator. The condition is written in parentheses and can be arbitrarily long. The block of statements in the first curly brackets is true if the condition is true. The block in braces after the word “else” will be executed if the condition is false.

You can use simple form (3) when both pairs of brackets are omitted. But the programmer must understand the entire measure of responsibility. Writing braces is not expensive, but it is a guarantee that management under any condition does not get out of control.

Ternary form of conditional operator

Example (4) shows the most popular application of a simple condition. A brief if statement in PHP perfectly complements the isset () function. It is very convenient to check the receipt of data by GET (or POST), and in the absence of the expected value, set the variable to the default value.

Remember to limit the conditions to parentheses. You should know that the ternary operator is easily implanted into expressions. It should be understood that the result of the PHP if statement in a short record “on equal terms” is involved in the expression. If the expression contains various mathematical or string operators, then the result of the ternary “if” is the equal operand of the expression.

Uniqueness of the condition is the key to success

Any operator in modern imperative programming cannot be skipped. Everything is done in the way and only as intended by the programmer. In PHP, if (in short form and in full form) has the same level of responsibility.

The ternary option is the result. The full form is a redirection of control. In the first case, an application error will lead to data errors. In the second case, control will be transferred in the wrong direction.

Uniqueness of the condition

When in doubt, you should not use the PHP conditions in a short entry. If () {} else {} can be more visual and practical. The general rule guaranteeing a stable result: there is doubt - deprive him of the basis. First let there be a big, but extremely understandable solution.

When the range of conditions becomes obvious and unambiguously clear, you can implement it in PHP if in a short entry.

Source: https://habr.com/ru/post/K11801/


All Articles