Moguce je ukljuciti sadrzaj jednog PHP fajla u drugi PHP fajl (pre nego sto ga server izvrsi), sa include ili require iskazom.
Inlcude i require iskazi su identicni, osim nakon neuspeha:
Dakle, ako zelite da se izvrsenje nastaviti i prikazete korisniku izlaz, cak iako include fajl nedostaje, koristite include iskaz.
Inace, u slucaju CMS-a ili slozene PHP aplikacije, uvek koristite require iskaz da ukljucite kljucni fajl na tok izvrsenja. Ovo ce pomoći da se izbegne ugrozavanja sigurnosti i integriteta aplikacije, za svaki slucaj, ako jedan fajl slucajno nedostaje.
Ukljucivanje fajlova stedi puno posla. To znaci da mozete napraviti standardno zaglavlje, podnozje ili meni fajlove za sve vase web stranice. Zatim, kada je potrebno da azurirate meni, mozete azurirati samo meni include fajl.
Primer: recimo da hocete da napravite zajednicki meni koji ce biti na svim stranica vaseg web sajta. Uobicajena praksa za imenovanje fajlova koji se ukljucuju je da se koristi ekstenzija .php. Ovaj meni sacuvacemo kao meni.php.
U xampp folderu, u htdocs folderu, u PHPTuts folderu napravite jedan folder, recimo, sr i u njega sacuvajte fajlove index.php i meni.php.
<html>
<body>
<a href="http://www.example.com/index.php">Pocetna</a> |
<a href="http://www.example.com/o-nama.php">O Nama</a> |
<a href="http://www.example.com/usluge.php">Usluge</a> |
<a href="http://www.example.com/kontakt.php">Kontakt</a> <br />
Ovaj fajl sacuvajte kao meni.php.
U istom folderu gde ste sacuvali meni.php, sacuvajte i index.php koji ce da sadrzi include iskaz, za ukljucivanje menija.
<?php include("meni.php"); ?>
<p>Pocetna stranica mog web sajta, koja koristi zajednicki
meni da mi ustedi vreme kada azuriram ili dodajem nove
stranice na moj web sajt.</p>
</body>
</html>
Ovaj fajl sacuvajte kao index.php.
Rezultat:
Primer 2: kada ukljucujete fajl sa include iskazom i PHP ne moze da pronadje taj fajl prikazace se sledeca greska:
U xampp folderu, u htdocs folderu, u PHPTuts folderu napravite jedan folder, recimo, include-iskaz i u njega sacuvajte fajlove index.php i meni.php.
<?php
include("fajl-ne-postoji.php");
echo "Ucim PHP.";
?>
Ovaj kod sacuvajte kao index.php.
Fajl meni.php je isti kao iz Primera 1. Prekopirajte ga u include-iskaz folder.
Rezultat:
Objasnjenje: echo iskaz se izvrsio, zato sto upozorenje (Warning) ne sprecava PHP skript da se pokrene.
Primer 3: kada ukljucujete fajl sa require iskazom i PHP ne moze da pronadje taj fajl prikazace se sledeca greska:
U xampp folderu, u htdocs folderu, u PHPTuts folderu napravite jedan folder, recimo, require-iskaz i u njega sacuvajte fajlove index.php i meni.php.
<?php
require("fajl-ne-postoji.php");
echo "Ucim PHP.";
?>
Ovaj kod sacuvajte kao index.php.
Fajl meni.php je isti kao iz Primera 1. Prekopirajte ga u require-iskaz folder.
Rezultat:
Objasnjenje: echo iskaz se nije izvrsio, zato sto je izvrsenje skripte prekinuto nakon sto je require iskaz vratio fatalnu gresku.
Preporuka je da se koristi require iskaz umesto include, zato sto vas skript ne bi trebalo da se izvrsi ukoliko fajl nedostaje ili je pogresno napisan,
It is possible to include the contents of a PHP file into another PHP file (before the server executes it), with include or require statement.
Inlcude and require statements are identical, except after failure:
So, if you want to continue the execution and to display output to user, even if the include file is missing, use the include statement.
Otherwise, in the case of CMS or complex PHP applications, always use the require statement to include a key file to the execution flow. This will help to avoid endangering the security and integrity of the application, just in case, if one file accidentally is missing.
Including files saves a lot of work. This means that you can make a standard header, footer, or menu files for all of your web pages. Then, when you need to update the menu, you can update only menu include file.
Example: let's say you want to create a common menu that will be on all the pages of your web site. The usual practice for naming files that are included is to use the .php extension. This menu we will save as menu.php.
In the xampp folder, in htdocs folder, in PHPTuts folder, make one folder, for example, en and in it, save the files index.php and menu.php.
<html>
<body>
<a href="http://www.example.com/index.php">Home</a> |
<a href="http://www.example.com/about.php">About Us</a> |
<a href="http://www.example.com/services.php">Services</a> |
<a href="http://www.example.com/contact.php">Contact</a> <br />
Save this file as menu.php.
In the same folder where you saved menu.php, save index.php which will contain the include statement, for including the menu.
<?php include("menu.php"); ?>
<p>Home page of my website, using common
menu to save me time when I'm updating or adding new
page on my website.</p>
</body>
</html>
Save this file as index.php.
Result:
Example 2: when you are including file with include statement and PHP can not find the file, the following error will show:
In the xampp folder, in htdocs folder, in PHPTuts folder, make one folder, for example, include-statement and in it, save the files index.php and menu.php.
<?php
include("file-does-not-exist.php");
echo "I am learning PHP.";
?>
Save this code as index.php.
File menu.php is the same as in Example 1. Copy it to include-statement folder.
Result:
Explanation: echo statement is executed, because a warning (Warning) does not prevent a PHP script to run.
Example 3: when you are including file with require statement and PHP can not find the file, the following error will show:
In the xampp folder, in htdocs folder, in PHPTuts folder, make a folder, for example, require-statement and in it, save the files index.php and menu.php.
<?php
require("file-does-not-exist.php");
echo "I am learning PHP.";
?>
Save this code as index.php.
File menu.php is the same as in Example 1. Copy it to the require-statement folder.
Result:
Explanation: echo statement was not executed, because the script execution stopped after the require statement returned a fatal error.
The recommendation is to use require instead of include statement, because your script should not be executed if a file is missing or misspelled.
Inlcude i require iskazi su identicni, osim nakon neuspeha:
- include ce proizvesti samo upozorenje i skripta ce se nastaviti
- require ce proizvesti fatalnu gresku i zaustaviti skriptu
Dakle, ako zelite da se izvrsenje nastaviti i prikazete korisniku izlaz, cak iako include fajl nedostaje, koristite include iskaz.
Inace, u slucaju CMS-a ili slozene PHP aplikacije, uvek koristite require iskaz da ukljucite kljucni fajl na tok izvrsenja. Ovo ce pomoći da se izbegne ugrozavanja sigurnosti i integriteta aplikacije, za svaki slucaj, ako jedan fajl slucajno nedostaje.
Ukljucivanje fajlova stedi puno posla. To znaci da mozete napraviti standardno zaglavlje, podnozje ili meni fajlove za sve vase web stranice. Zatim, kada je potrebno da azurirate meni, mozete azurirati samo meni include fajl.
Primer: recimo da hocete da napravite zajednicki meni koji ce biti na svim stranica vaseg web sajta. Uobicajena praksa za imenovanje fajlova koji se ukljucuju je da se koristi ekstenzija .php. Ovaj meni sacuvacemo kao meni.php.
U xampp folderu, u htdocs folderu, u PHPTuts folderu napravite jedan folder, recimo, sr i u njega sacuvajte fajlove index.php i meni.php.
<html>
<body>
<a href="http://www.example.com/index.php">Pocetna</a> |
<a href="http://www.example.com/o-nama.php">O Nama</a> |
<a href="http://www.example.com/usluge.php">Usluge</a> |
<a href="http://www.example.com/kontakt.php">Kontakt</a> <br />
Ovaj fajl sacuvajte kao meni.php.
U istom folderu gde ste sacuvali meni.php, sacuvajte i index.php koji ce da sadrzi include iskaz, za ukljucivanje menija.
<?php include("meni.php"); ?>
<p>Pocetna stranica mog web sajta, koja koristi zajednicki
meni da mi ustedi vreme kada azuriram ili dodajem nove
stranice na moj web sajt.</p>
</body>
</html>
Ovaj fajl sacuvajte kao index.php.
Rezultat:
Primer 2: kada ukljucujete fajl sa include iskazom i PHP ne moze da pronadje taj fajl prikazace se sledeca greska:
U xampp folderu, u htdocs folderu, u PHPTuts folderu napravite jedan folder, recimo, include-iskaz i u njega sacuvajte fajlove index.php i meni.php.
<?php
include("fajl-ne-postoji.php");
echo "Ucim PHP.";
?>
Ovaj kod sacuvajte kao index.php.
Fajl meni.php je isti kao iz Primera 1. Prekopirajte ga u include-iskaz folder.
Rezultat:
Objasnjenje: echo iskaz se izvrsio, zato sto upozorenje (Warning) ne sprecava PHP skript da se pokrene.
Primer 3: kada ukljucujete fajl sa require iskazom i PHP ne moze da pronadje taj fajl prikazace se sledeca greska:
U xampp folderu, u htdocs folderu, u PHPTuts folderu napravite jedan folder, recimo, require-iskaz i u njega sacuvajte fajlove index.php i meni.php.
<?php
require("fajl-ne-postoji.php");
echo "Ucim PHP.";
?>
Ovaj kod sacuvajte kao index.php.
Fajl meni.php je isti kao iz Primera 1. Prekopirajte ga u require-iskaz folder.
Rezultat:
Objasnjenje: echo iskaz se nije izvrsio, zato sto je izvrsenje skripte prekinuto nakon sto je require iskaz vratio fatalnu gresku.
Preporuka je da se koristi require iskaz umesto include, zato sto vas skript ne bi trebalo da se izvrsi ukoliko fajl nedostaje ili je pogresno napisan,
It is possible to include the contents of a PHP file into another PHP file (before the server executes it), with include or require statement.
Inlcude and require statements are identical, except after failure:
- include will produce only a warning and the script will continue
- require will produce a fatal error and will stop the script
So, if you want to continue the execution and to display output to user, even if the include file is missing, use the include statement.
Otherwise, in the case of CMS or complex PHP applications, always use the require statement to include a key file to the execution flow. This will help to avoid endangering the security and integrity of the application, just in case, if one file accidentally is missing.
Including files saves a lot of work. This means that you can make a standard header, footer, or menu files for all of your web pages. Then, when you need to update the menu, you can update only menu include file.
Example: let's say you want to create a common menu that will be on all the pages of your web site. The usual practice for naming files that are included is to use the .php extension. This menu we will save as menu.php.
In the xampp folder, in htdocs folder, in PHPTuts folder, make one folder, for example, en and in it, save the files index.php and menu.php.
<html>
<body>
<a href="http://www.example.com/index.php">Home</a> |
<a href="http://www.example.com/about.php">About Us</a> |
<a href="http://www.example.com/services.php">Services</a> |
<a href="http://www.example.com/contact.php">Contact</a> <br />
Save this file as menu.php.
In the same folder where you saved menu.php, save index.php which will contain the include statement, for including the menu.
<?php include("menu.php"); ?>
<p>Home page of my website, using common
menu to save me time when I'm updating or adding new
page on my website.</p>
</body>
</html>
Save this file as index.php.
Result:
Example 2: when you are including file with include statement and PHP can not find the file, the following error will show:
In the xampp folder, in htdocs folder, in PHPTuts folder, make one folder, for example, include-statement and in it, save the files index.php and menu.php.
<?php
include("file-does-not-exist.php");
echo "I am learning PHP.";
?>
Save this code as index.php.
File menu.php is the same as in Example 1. Copy it to include-statement folder.
Result:
Explanation: echo statement is executed, because a warning (Warning) does not prevent a PHP script to run.
Example 3: when you are including file with require statement and PHP can not find the file, the following error will show:
In the xampp folder, in htdocs folder, in PHPTuts folder, make a folder, for example, require-statement and in it, save the files index.php and menu.php.
<?php
require("file-does-not-exist.php");
echo "I am learning PHP.";
?>
Save this code as index.php.
File menu.php is the same as in Example 1. Copy it to the require-statement folder.
Result:
Explanation: echo statement was not executed, because the script execution stopped after the require statement returned a fatal error.
The recommendation is to use require instead of include statement, because your script should not be executed if a file is missing or misspelled.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.