+ 2

Hi all, i need an example of require in php i know what it is but i dont know how i can use it

11th Feb 2017, 1:04 PM
ehsan shahbazi
ehsan shahbazi - avatar
4 ответов
+ 2
anyone is here?
12th Feb 2017, 5:18 AM
ehsan shahbazi
ehsan shahbazi - avatar
+ 2
Simple example: When you are making some kind of login system, you need PHP & MySQL. Now, whenever you work with database, firstly you need to establish a connection with it. So for that purpose, you will create a small .php file (ex. connection.php) Now, whenever you want to interact with your database, you need access to it. We get an access by connection.php file, but we make this file separately and not inside some other file, you know why? Simply because whenever we need it, we just 'require' it inside our page we work in, less lines of code and more organised code as well. We used 'require' over 'include' because if the connection with the server doesn't exist, we might want to restrict the user from accesing that page. Yeah, the error will pop up as well telling the user 'action that user made was not successfully executed' -> Probaby because of failed connection to the database. If you used 'include' in this situation, no errors will pop up but the 'piece of code' we want to execute and interact with database will not run as well. No matter if there are no errors displayed at all. User will think everything run well (because of no errors) but not. :) Hope you understand it now a bit better.
12th Feb 2017, 9:49 PM
Dev
Dev - avatar
+ 2
There you go: At very top of your page where you want to require some file, you need to write: <?php require('some_file.php'); ?>
13th Feb 2017, 10:07 AM
Dev
Dev - avatar
+ 1
@Dev: thank you but i know its uses but i dont know how i should write it in code can you write a simple example for me thank you...
13th Feb 2017, 6:57 AM
ehsan shahbazi
ehsan shahbazi - avatar