If conditions in PHP: shorthand

Table of contents:

If conditions in PHP: shorthand
If conditions in PHP: shorthand

Conditions occupy a significant part of 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 short) was not so much recognized as it became the progenitor of the original conditionals. Many programmers feel it is their duty to express their own style in "making" original forms of condition expressions.

General form of the conditional operator

Conditions are the oldest syntactic construction. Along with the assignment operator, a simple action operator, conditions have come a long way in programming in general.

Choosing the Right Path
Choosing the Right Path

Before the advent of the younger "brother" of the switch / case statement, a lot of water has flowed under the bridge, programmers in various languages composed cumbersome constructions from multi-line if statements.

By the time PHP was introduced, programmers had gained experience in generating correct conditions. The idea of PHP, a server language destined by definition to be compact and fast, quickly came tooptimal syntax and forms of expression of 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.

Example of using if
Example of using if

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 statement block in the first curly braces is executed if the condition is true. The block in curly brackets after the word "else" will be executed if the condition is false.

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

Ternary form of the conditional operator

Example (4) shows the most popular use of a simple condition. PHP's if shorthand complements the isset() function perfectly. It is very convenient to check the receipt of data via GET (or POST), and if the expected value is missing, set the variable to the default value.

Don't forget to delimit the conditions with parentheses. You should be aware that the ternary operator is easily implanted into expressions. It should be understood that the result of the PHP if statement in the short notation "on equal terms" participates in the expression. If the expression contains various mathematical or string operators, then the result of the ternary "if" is an equal operand of the expression.

Uniquenessconditions are the key to success

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

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

Condition uniqueness
Condition uniqueness

When in doubt, do not use PHP terms in shorthand. If () {} else {} might be more descriptive and practical. The general rule that guarantees a stable result: if there is a doubt - deprive it of the basis. Let first be a big, but very understandable solution.

When the range of conditions becomes obvious and unambiguous, you can implement if in PHP in a short notation.

Popular topic

Editor's choice