Sunday, September 15, 2013

Modularity

A good programming large programs should be broken down into small programprogram hereinafter called modules . Small modules that can be invoked at any time required . In PHP also supports the concept of modularity , hereinafter named . We can insert the contents of a file / another module to the file / module specific . There are 2 command / function for that in PHP is using include and require .
Include ( )For ease of understanding , the following example is given . Suppose we are going to make a menu link index number is 4 pieces , about , links , and contact on every web page that we created . The technique used is to create a menu link in a separate module and then the module is invoked at any desired web page there is a menu link in it .menu.php


Home < / a> About Us < / a> Links < / a> Contact Us < / a>index.php< ? phpinclude (" menu.php " ) ;? > This is the index page < / p >< / body>< / html >about.php< ? phpinclude (" menu.php " ) ;? > This is a page about < / p >< / body>< / html >
From the above it appears the technique easily create web pages . In this case , we do not need to create a menu link on any existing web page . Imagine if we had a total of 100 pieces of web pages who all want to be tanpan menu link using the above technique , of course it is very troublesome . Although technically , web builder code broken into modules , but when dibrowser be seen together . Here is the HTML code generated by the browser when it opens a web page index.php


Home < / a> About Us < / a> Links < / a> Contact Us < / a> This is the index page < / p >< / body>
< / html >
Require ( )How to use and function of require ( ) together with include ( ) . So what's the difference ?Which should we use ? Consider the following example

< ? phpinclude (" noFileExistsHere.php " ) ;echo " Hello World ! " ;? >
 assuming that no noFileExistxHere.php file .Then by using the include ( ) will be generated display :Warning : main ( noFileExistsHere.php ) : failed to open stream: No such file or directory in include.php on line 2Warning : main ( ) : Failed opening ' noFileExistsHere.php ' for inclusion 
( include_path = ' . :/ usr / lib / php :/ usr / local / lib / php ' ) in include.php online 2Hello WorldNext we will use require ( ) .

< ? phprequire ( " noFileExistsHere.php " ) ;echo " Hello World ! " ;? >and resultsWarning : main ( noFileExistsHere.php ) : failed to open stream: No suchfile or directory in require.php on line 2Fatal error : main ( ) : Failed opening required ' noFileExistsHere.php '( include_path = ' . :/ usr / lib / php :/ usr / local / lib / php ' ) in require.php online 2
Compare the two results above , especially those printed in red . In the include () , errorWarning alone produced only in the form and still be able to run the next statement . it isseen that the text Hello World ! Still displayed in the browser . While the require ( ) ,error that produced a Fatal Error . Thus the next statement will not beexecuted .It is recommended that you use require ( ) in the hope that the PHP code that youmake will not be processed if there are files missing or do not exist.
 

news sources from :Rosihan Ari Yuana

No comments:

Post a Comment