You are on page 1of 7

PHP 

Include and Require Files
The include() and require() statement allow you to include the code contained in a PHP file within another PHP file. Including a file
produces the same result as copying the script from the file specified and pasted into the location where it is called.

You can save a lot of time and work through including files — Just store a block of code in a separate file and include it wherever you want
using the include() and require() statements instead of typing the entire block of code multiple times. A typical example is including the
header, footer and menu file within all the pages of a website.

The basic syntax of the include() and require() statement can be given with:


include("path/to/filename"); -Or- include "path/to/filename";
require("path/to/filename"); -Or- require "path/to/filename";

Example for include() function

<?php

echo "current file name is current_file.php";

include ("external_file.php");

?>

require() function

require() function का इस्तेमाल include() function के जैसा ही होता है |

<?php

echo "current file name is current_file.php";

include ("external_file.php");

?>

Difference Between include and require Statements


You might be thinking if we can include files using the include() statement then why we need require(). Typically the require() statement
operates like include().

The only difference is — the include() statement will only generate a PHP warning but allow script execution to continue if the file to be
included can't be

working with files:

 Opening a file
 Reading a file
 Writing a file
 Closing a file
fopen() – PHP fopen() function is used to open a file. First parameter of fopen() contains name of the file which is to be opened
and second parameter tells about mode in which file needs to be opened, e.g.,

<?php

$file = fopen(“demo.txt”,'w');

?>

Files can be opened in any of the following modes :


 “w” – Opens a file for write only. If file not exist then new file is created and if file already exists then contents of file is
erased.
 “r” – File is opened for read only.
 “a” – File is opened for write only. File pointer points to end of file. Existing data in file is preserved.
 “w+” – Opens file for read and write. If file not exist then new file is created and if file already exists then contents of file
is erased.
 “r+” – File is opened for read/write.
 “a+” – File is opened for write/read. File pointer points to end of file. Existing data in file is preserved. If file is not there
then new file is created.
 “x” – New file is created for write only.
2) fread() –– After file is opened using fopen() the contents of data are read using fread(). It takes two arguments. One is file
pointer and another is file size in bytes, e.g.,
<?php

$fp=fopen("data.txt", "r"); // r - read mode

$s=fread($fp,20);

echo $s;

?>

3) fwrite() – New file can be created or text can be appended to an existing file using fwrite() function. Arguments for
fwrite() function are file pointer and text that is to written to file. It can contain optional third argument where length of
text to written is specified.
For Example
<?php

$fp=fopen("data.txt", "w"); // w - write mode

fwrite($fp,"Infotect Computer Center")

?>

fclose() – file is closed using fclose() function. Its argument is file which needs to be closed, e.g.,

<?php

$file = fopen("data.txt", 'r');

//some code to be executed

fclose($file);
?>

. Create a New Directory


We use the mkdir() function to create a new directory in the PHP programming script.

<?php

mkdir("abc"); // mkdir function used to creating


directory/folder

?>

Delete

The rmdir() function in PHP is an inbuilt function which is used to remove an empty

directory. It is mandatory for the directory to be empty, and it must have the relevant

permissions which are required to delete the directory.

The directory to be deleted is sent as a parameter to the rmdir() function and it returns

True on success or False on failure.

<?php

rmdir("abc"); // rmdir function used to delete directory/folder

?>

changing a directory
The chdir() function is used to change the current working directory to a specified dir.

<?php

echo getcwd() .'<br>';

echo "Change to the 'test' directory".'<br>';

chdir('test');

echo getcwd() ;

?>
PHP File Upload
PHP allows you to upload single and multiple files through few lines of code only.

PHP file upload features allows you to upload binary and text files both. Moreover, you can have the full control over the file to be uploaded
through PHP authentication and file operation functions.

PHP $_FILES

The PHP global $_FILES contains all the information of file. By the help of $_FILES global, we can get file name, file type, file size, temp file
name and errors associated with file.

Here, we are assuming that file name is filename.

$_FILES['filename']['name']

returns file name.

$_FILES['filename']['type']

returns MIME type of the file.

$_FILES['filename']['size']

returns size of the file (in bytes).

$_FILES['filename']['tmp_name']

returns temporary file name of the file which was stored on the server.

$_FILES['filename']['error']

returns error code associated with this file.

move_uploaded_file() function

The move_uploaded_file() function moves the uploaded file to a new location. The move_uploaded_file() function checks internally if the file is
uploaded thorough the POST request. It moves the file if it is uploaded through the POST request.

  uploadform.html
<form action="uploader.php" method="post" enctype="multipart/form-data">  
    Select File:  
    <input type="file" name="fileToUpload"/>  
    <input type="submit" value="Upload Image" name="submit"/>  
</form>  
uploader.php
<?php  
$target_path = "e:/";  
$target_path = $target_path.basename( $_FILES['fileToUpload']['name']);   
  
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) {  
    echo "File uploaded successfully!";  
} else{  
    echo "Sorry, file not uploaded, please try again!";  
}  
?>  

Introduction to object oriented programming with PHP

OBJECT-ORIENTED PROGRAMMING IN
PHP
PHP is a server-side scripting language, mainly used for web development but also used as a general-purpose programming language. Object-Oriented
Programming (PHP OOP), is a type of programming language principle added to php5,
that helps in building complex, reusable web applications.

The Object Oriented concepts in PHP are:

 Class − This is a programmer-defined data type, which includes local functions


as well as local data. You can think of a class as a template for making many
instances of the same kind (or class) of object.
 Object − An individual instance of the data structure defined by a class. You
define a class once and then make many objects that belong to it. Objects are
also known as instance.
 Inheritance − When a class is defined by inheriting existing function of a parent
class then it is called inheritance. Here child class will inherit all or few member
functions and variables of a parent class.
 Polymorphism − This is an object oriented concept where same function can be
used for different purposes. For example function name will remain same but it
make take different number of arguments and can do different task.
 Overloading − a type of polymorphism in which some or all of operators have
different implementations depending on the types of their arguments. Similarly
functions can also be overloaded with different implementation.
 Data Abstraction − Any representation of data in which the implementation
details are hidden (abstracted). * Encapsulation − refers to a concept where we
encapsulate all the data and member functions together to form an object.
 Constructor − refers to a special type of function which will be called
automatically whenever there is an object formation from a class.
 Destructor − refers to a special type of function which will be called automatically
whenever an object is deleted or goes out of scope.

<?php
class Test

private $x,$y,$z;

public function getData()

$this->x=10;

$this->y=20;

public function sum()

$this->z=$this->x+$this->y;

echo "z=".$this->z;

$ob=new Test();

$ob->getData();

$ob->sum();

?>

CONSTRUCTOR

o PHP 5 allows developers to declare constructor methods for classes.


o Constructor is suitable for any initialization that the object may need before it is used.
o We can design constructor using "__construct" or same name as class name.
o Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call
to parent::__construct().

<?php
class Test
{
private $x,$y,$z;
public function __construct() // constructor
{
$this->x=10;
$this->y=20;
}
public function sum()
{
$this->z=$this->x+$this->y;
echo "z=".$this->z;
}
}

$ob=new Test();

$ob->sum();
?>

You might also like