PHP – goto statement

The goto statement plays an important role in PHP if we want to take control to another section of the program.

The goto statement in PHP

In the goto statement, the target location is specified using  a label followed by a colon, and the control is specified as goto followed by the desired target label.

You can understand the goto statement using the following example:

 

PHP – include_once statement

The include_once statement in PHP is almost similar to include statement as we have already read about the include statement in previous chapter. But in case of include_once, it  will ensure that the file is included only once in the program and in case, the file doesn’t exists then it will just generate a warning and execute the program just like the include statement.

You can understand this using the following example:

Difference between include_once and require_once statements in PHP

Both the statements will ensure that the file is included only once in the program. But the main difference is that in case of require_once if file doesn’t exists then the program will generate a fatal error and it will stop the program execution immediately but in case of include_once, it will just display a warning message and execute the program.

Now I hope you are all clear about include, include_once and require, require_once.

PHP – require_once statement

The require_once statement is almost similar to require statement except PHP will check if the file has already been included in the program or not, and if the file already included in the program then it will not include again in the program.

The require_once statement in PHP

So if you want to be sure that the should be included only once then you should only use the require_once statement to include any file in the program.

You can understand the require_once statement using the following example:

The require_once statement in PHP will ensure that the file must be included only once and if in case, the file doesn’t exists it will generate the fatal error and program execution will stop working immediately.

PHP – include statement

The include statement in PHP is also used to include any external file in the program. But if we use include statement then it will generate just a warning and execute the program and will display the results as per the input value.

The include statement in PHP

You can understand the include statement using the following example:

Difference between include and require statement in PHP

The function of both the statements are almost similar except in case of require statement, if file doesn’t exists then it will generate a fatal error and it will stop the program execution immediately but in case of require statement, it will just check the file and if it doesn’t exists then it will just display a warning and execute the program.

So if we want to be a file is included in the program then we should use the require statement only.

Now I hope you are all clear about the include and require statements.

In next chapter, we will see how to use require_once and include_once.

PHP – require statement

The require statement in PHP is used to include any external file in our program.

require statement in PHP

we can understand the require statement using the following example:

As require is not a PHP function, so we don’t need to use parenthesis (). So we can also use the require statement in the following way:

The require statement is PHP is mainly used if it is compulsory to use any external file and if we use the require statement to include that file then it will ensure that the file should be included in the program otherwise the program will generate a fatal error and execution will stop immediately.

PHP – return statement

The return statement in PHP is used to stop the program execution and returns the program control in the beginning of the program.

The return statement in PHP can be used in several ways:

  1. if return statement is called in a function, then it will stop the program execution immediately and return its argument as a value of the program.
  2. The return statement can also ends the execution of any eval() statement or script file.
  3. If in case, the current script file is included or required, then control will be passed back to the calling file.
  4. And if no parameter is passed in the return statement then it will return a NULL value.

Example1 for return statement in PHP

Example2 for return statement in PHP

Example3 for return statement in PHP

 

PHP – declare statement

The “declare” statement in PHP is used to set execution directives for a block of code in the program. The syntax of declare is  almost similar to other control constructs as given below:

The directive section allows the behavior of the declare block to be set. Currently there are only three directives:

  1. the ticks directive
  2. the encoding directive
  3. the strict_types directive

the ticks directive in PHP

A tick in declare statement is an event that occurs for every “n”  low-level tickable statements executed by the PHP parser within the declare statement block. The value of “n will be  specified using ticks=n within the declare directive section.

Example 1:

Example 2

the encoding directive in PHP

Any script’s encoding can be specified/declared as per-script using the encoding directive. You can understand this with the following example:

Example:

the strict_types directive in PHP

In this directive, a variable of exactly same type of the type declaration will be accepted otherwise a TypeError will be thrown. If you want to enable strict mode, then the declare statement is used with the strict_types declaration.

Example:

 

PHP – continue statement

The continue statement is used if we want to stop the execution and returns the control in the begging of the loop. The continue statement mainly used in PHP loops like for loop, while loop, do…while loop etc.

Example

The difference between break and continue statement in PHP

The main difference between break; and continue statement is that the break; statement just stop the execution of the program and just come out of the loop immediately instead in continue; statement, it will just skip the current execution of the program and control returns in the beginning of the program.

PHP – break statement

We have used the “break;” keyword in our switch statement to stop the execution of the program once the match the condition.

So the PHP break statement will terminate the program immediately and come out of the loop without wasting any more time in execution. So the main benefit of break; statement is to make the program execution faster.

So we can  use break; keyword in loops like for loop, while loop, do…while loop, foreach loop and switch statement.

Example 

 

PHP – $_GET/$_POST Methods

Forms are the most commonly used to send and received enquiries on any website. So there are two methods which are mainly used to send information from browser client to the server.

The $_Get Method in PHP :

When we use $_GET method in any form, the browser will append all the form values in the page page URL as a query string. All the form values will be added after the (?) character and if there is any space in the fields values and browser will automatically will replace the space + sign and pass all the values on the form action page. You can see this using the following example:

Example:

The $_POST method in PHP

$_POST is another most important method to pass the form values to the server. In this method, all information is sent using the HTTP headers.

Example:

Difference between $_GET and $_POST methods

  • In $_GET method, all form values are sent as a query string instead in $_POST all values are sent using HTTP headers.
  • $_GET method has some limit (upto 1024 characters) in passing the form values instead $_POST method doesn’t have any restriction in variable passing.
  • $_GET method is not much secure because all values are sent as query string instead $_POST method is highly secured method.
  • $_GET method can be used to send binary data like images etc. instead $_POST can send all kind of data in a very secured way.