PHP construction if else: hidden logic

The conditional construction in the implementation from PHP if else is practically no different from the similar rules of other programming languages, and you will not have to expect any special differences for a long time.

php if else

In ancient times, the assignment operator in the Pascal language was ": =", while C / C ++ was confidently controlled by one "=". To this day, the PHP condition if else can use three "strange" options: "=", "==", "===". It is also strange to use the โ€œ$โ€ symbol to denote a variable (JavaScript does fine without variable refinement characters). Meanwhile, the growing number of programmers using PHP everyday is the best evidence of the practicality of the language.

The essence of conditional construction

Conditions determine the branching of the algorithm. The condition is based on an expression whose execution result is always only true or false . Any number of operators and function calls may be used in the contents of this expression. This means that a condition is, first of all, an algorithm.

Historically, a conditional expression is written like this:

  • (expression1) [comparison symbol] (expression2).

This moment enables the second expression to use the results of the first. Few people attach importance to this, but every programmer sometimes forgets that the assignment symbol "=" is not a comparison symbol "==", and the difference between "==" and "===" is a good reason to rewrite the condition in such a way so that you can apply an unambiguous comparison of "<" or ">".

php if eise examples

Here's the construction of PHP if else - as examples of the โ€œcollaborationโ€ of two expressions. The first expression increments the variable $ iA (= 2) and evaluates the variable $ iC (= 6) in the scfAB () function. The second expression has a modified value of $ iC, which multiplies by 20 and increments the variable $ iB.

Using "=", "==" and "==="

In PHP, the if else construct does not imply the use of the "=" operator, since it is an assignment operator, but its use is still possible. It is important to understand that the erroneous indication "=" instead of "==" in most cases will make the condition true, regardless of what is written in it.

php condition if else

For PHP, if () else is supposed to use only โ€œ==โ€ or โ€œ===โ€. The first denotes the equality of values โ€‹โ€‹after the conversion of types, the second counts on the correspondence of values โ€‹โ€‹and their types. The rest of the operators (more, less, not equal ...) do not cause an ambiguous interpretation.

Characteristics of characters and their application

The description of variables and the conditional construction in PHP if else allow us to formulate a problem that has existed for a long time and has become so familiar that it is almost not noticeable.

By and large, the comparison operator is not particularly needed. Firstly, in every language there is an expression execution construct (PHP is not an exception), which is defined dynamically during script execution. Secondly, absolutely nothing prevents the formation of a branching algorithm during the execution of the main algorithm.

php condition if else

It's just that PHP if else , like its younger brother switch case, is familiar and perceived by both programmers and users who are used to controlling the work of programmers.

A typical mistake, which is often made: โ€œ=โ€ or โ€œ==โ€ - in the condition, the second is permissible, but not the first (unless it is an exception to the rule).

The "$" symbol for describing a variable is redundant. If in one language you can do without additional identification of the variable name, then in PHP it can also be done. It has become traditional to separate "=" from "==". The first is assignment, the second is comparison.

Meanwhile, as soon as the PHP interpreter โ€œentered theโ€ if else construct , there is no doubt that there will be a condition. Moreover, the condition on the syntax of the PHP language: if () else is always in brackets, that is, it is always possible to identify โ€œ=โ€ as a comparison operator.

Another question is that the use of "$" and the triad: "=", "==" and "===" has become habitual, and changing something is unpromising. But, manipulating the condition, it is advisable to keep in mind the above. There are a lot of classical constructions in modern languages, but the time is coming when it is necessary to reconsider their meaning.

Programming takes on the status of a serious tool for processing dynamically changing information, and this goes beyond understanding why, inside and outside the PHP if else construct, the "=" operator retains its meaning.

Conditions and objects

From the moment that objects became natural for any development, the conditional construction of PHP if else was preserved, but its place was shifted to the body of the object. The programmer can use the conditions, as it is convenient for him, but, developing objects, he translates the moment of decision-making to the object.

php construct if else

The system of application objects is the logic of interaction, that is, making decisions about how exactly the object takes the work further. If a conditional construct manipulates objects, and not objects control the algorithm, then something has been done wrong in the script.


All Articles