The PHP language (or PHP) is quite easy to learn and therefore attractive to beginners. The language has a number of shortcomings, but it confidently occupies a leading position in its niche - web development. Among programmers, there are often disputes about the methods of using this language, about the cleanliness of the code of the project being created, and so on. But this is all from the field of methodology, you will certainly participate in such discussions in due time, but first you need to get basic knowledge.

php programming for dummies



















This article continues the topic of PHP programming for dummies , started in the first article - The PHP Language for Dummies . In a previous article, I suggested several ways to set up a local web server. You've got a basic understanding of what file to put PHP code in and how to do it correctly. We also covered issues such as data output, variables, language constants, and the types of data contained in these variables. It's time to move on and move on to PHP programming for already not quite complete dummies)

Expressions and operations

  1. An operation is an action that can be performed on variables.
  2. An expression is a group of sequentially (according to priority) operations performed.
  3. An operand is either a variable or a specific value.

Operation Priority

With highest priority:

  • ( ) – function call
  • [ ] – indexing (referring to an array element)

Single operations

  • - logical negation
  • + - single plus
  • - - single minus
  • ++ - increment prefix notation
  • --- decrement prefix notation

Multiplicative

  • * - multiplication
  • / - division
  • % - modulo division

Additive

  • + - double plus
  • — — double minus

Relations

  • < - less
  • > - more
  • <= - less than or equal to
  • >= - greater than or equal to

Equivalence

  • == - equality test
  • != – inequality check
  • === - identically equal
  • !=== — not equal to identical

brain teaser

  • && or and - logical AND;
  • || or or - logical OR;

Assignments

  • =
  • *=
  • /=
  • %=
  • +=
  • -=

Postfix increment and decrement

Conditional if statement

  1. The conditional statement is used to control the execution of a script. Condition statement syntax:
if(condition) {statement block}
  1. Execution scheme: if the condition is true, then the statement block is executed, if the condition is false, then the statement following the if is executed.
  2. With the following execution scheme, if the condition is true, then statement block1 is executed, and if the condition is false, then statement block2 is executed:
if(condition) {statement block1}
else {statement block2}
  1. Instead of the else keyword, you can use elseif(condition) to set up another condition to be checked:
if(condition1) {code that is executed if condition1 is true}
elseif(condition2) {code to execute if condition1 is false and condition2 is true}
else {code to be executed if condition1 and condition2 are false}
  1. There may be several elseif constructs in an if statement, or none at all. Example:
< ?php $x=52; if($x>50) {echo $x;}
elseif($x==50) {echo $x+12;}
else {echo "hello";}
?>

switch-case statement

  1. This operator is the closest relative of the conditional operator, as it allows you to execute operators depending on the value of a variable (expression, function). Example:
switch(expression)
{
case value1: statements1;
case value2: statements2;

}
  1. Here is how this operator works. First, the value of the expression is evaluated. Suppose it is X. Then X, X + 1 and default statements will be executed. If a break statement is given after each group of statements, then only the X statements will be executed.
  2. If the computed value does not match any of the values ​​specified with case, then only the default statements will be executed, provided they are given. Here is an example using switch-case that outputs certain text depending on the value of $y:
< ?php switch($y) { case 1: echo "y=1"; break; case 3: echo "y=3"; break; case 5: echo "y=5": break; } ?>

Condition operator ?

  1. Condition statement syntax:
condition ? Expression1 : Expression2;
  1. With the assignment operator, the condition operator has the form:
variable = condition ? Expression1 : Expression2;
< ?php $x=5; $x<10 ? “x is less than 10” : “x is greater than or equal to 10”; echo $x; ?>

Loop statements

  1. Loop statements ensure that a block of code is repeatedly executed until a certain condition is met. PHP provides four loop statements: while, do-while, for, and foreach.
  2. The while loop is called a precondition loop because the condition is checked first and then, if the condition is true, the loop code is executed. Syntax:
while(condition) {code;}
< ?php //Loop example: $i=10; while($i>0) {
echo "$i";
$i--;
}
//The script will output: 10 9 8 7 6 5 4 3 2 1
?>
  1. The principle of the cycle, I think, is clear. First we check if $i is greater than zero. If so, we output its value and decrement it by 1.
  2. A do-while loop is called a conditional loop because the loop code is executed first and then the condition is checked. Syntax:
do {code;} while(condition);
<?php
//Example of using the loop
$i=1;
do {
echo "$i";
$i++;
} while($i<=10); //The loop will output: 1 2 3 4 5 6 7 8 9 10 ?>
  1. The for loop is useful when we know exactly how many iterations we need. For loop syntax:
for(initializer; condition; statement_after_iteration) {code;}
  1. The initializer is executed first. Typically, it sets the initial value of the loop counter. The condition is checked at each iteration of the loop. If it is true, then the loop code is executed. After each iteration, the statement specified after the condition is executed. Example:
<?php
for($i=l; $i<=10; $i++) echo "$i "; //The loop will output: 1 2 3 4 5 6 7 8 9 10 ?>
  1. The remaining foreach loop does not make sense now, since it is specifically designed to work with arrays. Instead, consider two special break and continue statements.
  2. The break statement interrupts the execution of the loop.
  3. The continue statement interrupts the execution of the current iteration of the loop.
  4. The break statement can be used to force the loop to stop, and continue to skip the current iteration. For example, this loop prints only odd numbers in the range from 1 to 10 (if $i is even, then we continue to the next iteration with continue):
<?php
for($i=0; $i<=10; $i++) { if ($i % 2 == 0) continue; else echo "$i"; } ?>

That's all for today. We can say that the program is at least finished! Description of other PHP language constructs is already beyond the scope of one article. Such areas of knowledge as arrays require a separate story.