PHP Commands and how are they used? | CodingSource

PHP Commands and how are they used?

In some cases, you can determine what should be decided with php commands. By creating a loop, it is possible to do with code what should happen in some conditions.

if command

The if command executes the following set of commands if a statement is true. The English meaning of the word "if" is in a unity with its meaning in terms of the task it undertakes.

<?

################

# if.php #

################


$a = 5;

$b = 3;

if ( $a > $b )

  { echo "a is greater than b."; }

?>

If the value in the $a variable is numerically greater than $b, the above piece of code will execute the code specified in the instruction set and print the text "a number is greater than b". Otherwise (that is, if the numeric value of $a is less than $b), the code in the instruction set is skipped without any action.

if... else ... command

In this command, as in the if command, if the specified statement is true, it executes the set of commands specified with if, and if the statement is false, the set of commands specified with else will be executed.

<?

###################

# if_else.php #

###################

$a = 2;

$b = 3;

 if ($a > $b) {

     echo "a is greater than b.";

 } else {

    echo "b is greater than a.";

 }

?>

If the logical expression in parentheses is correct, the command set following the if statement (the region delimited by the signs "{" and "}") is executed. If the numerical value of the variable specified by $a above is greater than the value specified by $b, "a is greater than b." Otherwise, if $a is numerically less than $b, the text "b is greater than a." text will be printed. The code specified in the instruction set can be edited for any operation, not just writing to the screen.

While command

Again, the English meaning of the word "during" is close to its meaning in terms of its task. Executes the desired set of commands as long as the condition specified in parentheses is true. The example below may seem a little confusing at first, but if you follow it carefully and step by step, you will realize that it is not so complicated.

<html>

<head><title>PHP used in HTML</title>

</head>

<body bgcolor="#ffffff" text="#000000">

<?

################

# while.php #

################

$a = 1;

while($a < 7):

 echo “<h$a>This is a title with h$a.</h$a><br>\n”;

 $a = $a + 1;

 endwhile;

?>

</body>

</html>

The example above is a good example of both the use of the while loop and the use of PHP in HTML. I could have written the while loop I used as follows.

<html>

<head><title>PHP used in HTML</title>

</head>

<body bgcolor="#ffffff" text="#000000">

<?

################

# while.php #

################

$a = 1;

while($a < 7)

{

 echo “<h$a>This is a title with h$a.</h$a><br>\n”;

 $a++;

}

?>

</body>

</html>

The result will still be the same. There is no difference in terms of usage, it is up to the programmer which one to use, but I always prefer the second usage in my examples.

An unfamiliar usage in the above example is the expression $a++ (The same usage exists in C) This expression is the same as $a = $a + 1 (It's a common expression in programming, but those who are inexperienced will find it difficult). This means "increment the value in $a by one and then assign that value as the new value of $a". So if $a = 1 then $a = 2 after $a++. Likewise in $a--, it is "decrease the value of $a and assign that value as the new value of $a".

In the example above, the variable $a is first assigned a value of 1 and converted into an HTML tag with the <h$a> expression. For the value $a = 1, this expression is <h1>, for $a = 2, this expression is <h2> .... With the while loop, it is stated that the last value $a can take is 6 (the moment $a < 7, the loop ends!). When $a takes the value 7, the specified condition becomes false (i.e. 0) and the loop is not executed. If the $a++ expression were not used, the program would go into an infinite loop, which means that the text specified by the <h1> tag is printed to the screen endlessly. (Actually, the endless loop words are not quite correct, as the PHP configuration file on the server system defaults to a maximum runtime of 30 seconds for PHP files). In the while loop, the condition is reviewed first, then the operations are performed.

do ... while command

The do...while loop has the same functionality as the while loop. There is only one difference. In the while loop the condition is checked without entering the loop whereas in the do....while loop the condition is checked at the end of the loop.

<?

$a = 0;

do {

   print $a;

} while($a > 0);

?>

If the above code was written with a normal while loop, that is, as follows

<?

$a = 0;

while($a > 0) {

   print $a;

}

?>

it would not give any output to the screen. However, in the first example, “0” will be printed on the screen. A loop created with do....while can also be created with while, and in my personal opinion the while loop is easier to use. Of course, this loop has its own uses, but I have always used the while loop for such loops in the scripts I wrote until now.

for command

The for loop does pretty much the same thing. I would like to explain its usage with an example that will give the same output as the example in the while loop.

<html>

<head><title>PHP used in HTML</title>

</head>

<body bgcolor="#ffffff" text="#000000">

<?

################

# for.php #

################


 for ($a = 1; $a < 7; $a++)

 {

  echo "<h$a>This is a header with h$a.</h$a><br>\n";

 }

?>

</body>

</html>

The above code also gives the same output as in the while loop. In the for loop, the initial value of the variable is written first, then the condition and finally the part that needs to be executed for the variable. So the pattern of use

for (initial_value;condition;operation)

is in the form.

Of course, the example I used is valid for all loops, you will encounter such situations that you have no alternative, so you can only use for or while loop. Anyway, don't look over it just because they're doing the same job, get a good grasp of it.

switch command

switch loops are actually nested if loops. But, from personal experience, in a loop where you use a lot of ifs, sometimes things get really tricky. For example, you will receive a value with a web form (with a drop-down menu). You want to understand what this value is. For this, a selection is made between the predefined values ​​in the menu. The switch loop is used for this selection. Save the files below with the names specified in the comments section and then open the menu.html file from your client.

<!-- save as menu.html -->

<html>

  <head>

    <title>Menu</title>

  </head>

  <body bgcolor="#ffffff" text="#000000">

    <form action="menu.php" method="post">

      <b>Choose your operating system</b><br>

      <select name="os_type" size="1">

        <option value="win">Windows 9x</option>

        <option value="winnt">Windows NT</option>

        <option value="linux">Linux</option>

        <option value="unix">UNIX</option>

        <option value="os2">OS/2</option>

        <option value="macos">MacOS</option>

      </select>

      <input type="submit" value="Query">

    </form>

  </body>

</html>

<?

// save as menu.php


switch($os_type)

{

  case "win" :

     echo “Your operating system is Windows 9x”;

     break;

  case "winnt" :

     echo "Your operating system is Windows NT";

     break;

  case "linux" :

     echo "Your operating system is Linux";

     break;

  case "unix" :

     echo "The operating system you are using is UNIX";

     break;

  case "os2" :

     echo "The operating system you are using is OS/2";

     break;

  case "macos" :

     echo "The operating system you are using is MacOS";

     break;

}

?>

The example above asks the user for the operating system he/she uses and runs the part specified in the loop in line with the response. The break command I use ensures that the program exits the loop if that part of the program is executed, that is, if the user has marked the operating system as Linux, Windows, WindowsNT and the latest Linux partition are executed. The text "The operating system you are using is Linux" is printed on the screen and the next command, break, is executed, the loop is exited with the execution of the break command, so no time is wasted by checking other values.

There is another feature used in the switch loop that I did not use in the following example: the default option.

This option is the part that will be executed if no other selection is correct. To give an example.

<?

// save as menu.php

$i = 5;

switch($i)

{

  case "2":

     echo "Value of 2";

     break;

  case "3":

     echo "The value is 3";

     break;

  default:

     echo "Value unknown";

}

?>

This code will display "Value unknown". Because no matter what value you give to the $i variable other than the 2 and 3 values ​​defined, the section outside these two conditions, that is, the section defined by default, will be operated.

PHP Codes Related Posts

Newer Post Load More Pages..