Tag Archives: php

PHP File

Use the fopen() function to open files in PHP. The first parameter of this function is the file to be opened and the second parameter specifies in which mode the file should be opened:

1
2
3
4
5
6
7
8
9
10
11
12
13
<html>

<body>

<?php

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

?>

</body>

</html>

The modes in which the file can be opened are listed below:

Modes Description
r Read only. Starts at the beginning of the file
r+ Read/Write. Starts at the beginning of the file
w Write only. Opens and clears the contents of file; or creates a new file if it doesn’t exist
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn’t exist
a Append. Opens and writes to the end of the file or creates a new file if it doesn’t exist
a+ Read/Append. Preserves file content by writing to the end of the file
x Write only. Creates a new file. Returns FALSE and an error if file already exists
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already

Note that if the fopen() function is unable to open the specified file, it returns 0 (false).To close an open file use the fclose() function:

1
2
3
4
5
6
7
8
9
<?php

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

//some code to be executed

fclose($file);

?>

To check if the “end-of-file” (EOF) has been reached use the feof() function.To loop through data of unknown length use the feof() function.Note that you cannot read from files opened in w, a, and x mode!To read a single line from a file use the fgets() function.Note that after a call to this function the file pointer has moved to the next line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

$file = fopen("welcome.txt", "r") or exit("Unable to open file!");

//Output a line of the file until the end is reached

while(!feof($file))

  {

  echo fgets($file). "<br />";

  }

fclose($file);

?>

PHP introduction

PHP is a powerful tool for creating interactive and dynamic web pages. To get skilled in PHP, you should have a basic understanding of HTML/XHTML and JavaScript.

PHP stands for Hypertext Preprocessor. This is a server-side scripting language (like ASP) which supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) As PHP is an open source software, it can be downloaded and used for free. PHP is easy to learn and runs efficiently on the server side. To get access to a web server with PHP support, you may install Apache (or IIS) on your own server, install PHP, and MySQL.

PHP file (“.php”, “.php3″, or “.phtml” file extensions) is returned to the browser as plain HTML. It can contain text, HTML tags and scripts.

PHP Operators

Arithmetic Operators

Operator Description Example Result
+ Addition x=2
x+2
4
- Subtraction x=2
5-x
3
* Multiplication x=4
x*5
20
/ Division 15/5
5/2
3
2.5
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0
++ Increment x=5
x++
x=6
Decrement x=5
x–
x=4

Assignment Operators

Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y

Comparison Operators

Operator Description Example
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
<> is not equal 5<>8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true

Logical Operators

Operator Description Example
&& and x=6
y=3
(x < 10 && y > 1) returns true
|| or x=6
y=3
(x==5 || y==5) returns false
! not x=6
y=3
!(x==y) returns true

Operators are used to operate on values. This section lists the different operators used in PHP.

PHP While Loops

The idea of a loop is to do something over and over again until the task has been completed.In PHP, we have the following looping statements:

  • while - loops through a block of code while a specified condition is true
  • do…while - loops through a block of code once, and then repeats the loop as long as a specified condition is true
  • for - loops through a block of code a specified number of times
  • foreach - loops through a block of code for each element in an array

The while loop executes a block of code while a condition is true.

Syntax

 while (condition)
    {
    code to be executed;
    }

The do…while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.

Syntax

 do
  {
   code to be executed;
    }
   while (condition);



        

PHP Include

The include() or require() function allows to insert the content of one PHP file into another PHP file before the server executes it. These two functions are used to create functions, headers, footers, or elements that will be reused on multiple pages.  include()  and  require()  functions are quite identical, except they handle errors in different ways. include() generates a warning, but the script will continue execution, while require() generates a fatal error, and the script will stop. Server side includes save a lot of time. You can create a standard header, footer, or menu file for all your web pages. When the header needs to be updated, you can just update the include file. And when you add a new page to your site, you don’t need to update the links on all your web pages. Just change the menu.