PHP Tutorial #19 ( Include and Require ) , Globals Variable, Request and $_SERVER Variables
Repository
https://github.com/php/php-src
What Will I Learn?
I will learn ( Include and Require ) to include pages, Globals Variable to solve the problem of the undefined variables, Request without precise method and $_SERVER Variables part 2.
- How to include a page in another page without rewriting it using " Include & Require ".
- How to use the $GLOBALS to be accessible throughout the page.
- The use of REQUEST in the place of GET and POST.
- The variables of $_SERVER, SERVER_ADDR, SERVER_NAME and SERVER_SOFTWARE.
Requirements
System Requirements:
- Server support PHP , Xampp or Wamp for example
- An IDE like Sublime text.
- Browser (Chrome for example)
OS Support for PHP
- Windows
- macOS
- Linux
Required Knowledge
- HTML language
- CSS language
Difficulty
- Basic
Description
1- Include and Require
- Include :
Includes the contents of another called file, but does not cause a crashing error if it is unavailable.
- Include-once :
The same as include, but only once and for all in the same document include was previously called with the same file name.
- Require :
Includes the contents of another file called, and causes a blocking error if it is unavailable.
- Require-once :
The same as require, but only once and for all in the same document, if require has already been called before with the same file name.
To use the include
or require
we need to pass the page as parameter, and the system will check the availability of this page , if it's true or if we have really this page in our device it will include it , else we have two cases :
If you are using include
you will get a warning ( the warning can be processed using some methods ) and the page will continue the execution.
If you are using require
you will get an error and the page will block the execution.
I have 3 HTML pages, I want to combine between them to get a signle page , this is the code
I have used include
and require
for the navbar, header and footer and this is the result
2- Globals Variable
The $GLOBALS
is an associative array containing references to all global variables currently defined in the global execution context of the script. The names of the variables are the indexes of the array.
Any variable to be accessible in all the page you need to use $GLOBALS
array with the name variable as key to get the value.
In my example I have two variables ' x and z ' I want to add x to z and print the result
The result will be 5 because the function will return x + z => 3 + 2 = 5.
3- Request
$ _REQUEST
is one of the superglobal variables of PHP. It can retrieve variables provided to the script by any input mechanism. In other words, it gracefully replaces $_POST
, $_GET
..etc.
However the data is recovered in a certain order, the one predefined by the 'variables_order' directive of the php.ini.
The use of this variable can therefore pose problems of operation / security at the level of script if you do not pay very attention to its use, that is why it is preferable to use the variables $_GET
, $_POST
to control the source of your data.
I will use the same example of the get
and post
, I will send data from the user to the server using the post method , the server will receive and processe the data using the $_REQUEST
When we enter Alexendre and Maxim for example the $_REQUEST
will receive them and return the result to the client
4- $_Server Variables
- Server Addr :
The $_SERVER ['SERVER_ADDR']
returns the IP address of the host server, an IP address is a unique number that allows a computer to communicate in a network.
- Server Name :
The $_SERVER ['SERVER_ NAME ']
returns the name or the URL of the host server.
- Server Software :
The $_SERVER ['SERVER_ NAME ']
returns the software of the host server , the software is found in the headers when responding to requests.
We need sometimes these variables to display some informations about the server, the software for example the version of PHP that the user knows and understands its uses, I have used echo to print the result of each variable
echo $_SERVER['SERVER_ADDR'];
echo "<br>" . $_SERVER['SERVER_NAME'];
echo "<br>" . $_SERVER['SERVER_SOFTWARE'];
And this is the result
Video Tutorial
Curriculum
- PHP Tutorial #11 String Methods ( Addslaches, Chr, Chop and Chunk_splite )
- PHP Tutorial #12 String Methods ( Bin2Hex, Count_Chars, Explode and Hex2Bin )
- PHP Tutorial #13 String Methods (Implode, Join, Lcfirst, Ltrim and Fprintf )
- PHP Tutorial #14 String Methods (Md5_File, Md5, Nl2br and Addcslaches )
- PHP Tutorial #15 String Methods (Number_Format, Ord, Parse_Str, Print and Printf )
- PHP Tutorial #16 String Methods (QuoteMeta, Rtrim , Sha1_file and Sha1 )
- PHP Tutorial #17 Mathematical Methods (Abs, Acos , Acosh and Asin )
- PHP Tutorial #18 ( GET and POST ) Methods and $_SERVER Variables
Proof of Work Done
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/rc1.php
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/page1.php
Hi @alexendre-maxim
Thank you for your contribution to the video category.
I see that you have made improvement in this video tutorial. You are spending more time to get your points across with clarity.
I also see that your fonts and the images shown in the videos are all in good size and can be easily followed by the students.
Keep up the good work.
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Chat with us on Discord.
[utopian-moderator]
Thank you for your review, @rosatravels! Keep up the good work!
Thank you @rosatravels , I am very happy to this.
Hey, @alexendre-maxim!
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
Thank you