‘PHP’ Category

 

What are PHP Files ?

PHP Files Introduction and Overview


In PHP Files are Used for Complex programming by programmers . Files are used for manipulating the taks in program code such read , write open, append, truncate, and upload .

PHP Supports :

PHP File Open

PHP File close

PHP File Write

PHP File Read

PHP File Truncate

PHP File Open

In PHP fopen() function is used to open files in PHP.

<html>
<body>
<?php
$file=fopen(“Hello.txt”,”r”);
?>
</body>
</html>
PHP File Modes

r Read only. Starts at the beginning of the file .place the file pointer at the beginning of the file.

r+ Read/Write. Starts at the beginning of the file .place the file pointer 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 .If the file does not exist, attempt to create it.

a+ Read/Append. Preserves file content by writing to the end of the file .place the file pointer at the end of the file. If the file does not exist, attempt to create it.

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
Closing PHP File

fclose() function is used to close an open file. Thus After opening file ,closing file seems good practice.

<?php
$file = fopen(“hello.txt”,”r”);
//some code to be executed
fclose($file);
?>
End- of – File In PHP

feof() function checks if the “end-of-file” (EOF) has been reached.
The fgets() function is used to read a single line from a file.

<?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);
?>

The fgetc() function is used to read a single character from a file.

<?php
$file=fopen(“welcome.txt”,”r”) or exit(“Unable to open file!”);
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>
PHP File Write

In PHP fwrite function allows data to be written to any type of file. Fwrite’s first parameter is the file handle and its second parameter is the string of data that is to be written.
$myFile = “helloFile.txt”;
$fh = fopen($myFile, ‘w’) or die(“can’t open file”);
$stringData = “Bobby Bopper\n”;
fwrite($fh, $stringData);
$stringData = “Tracy Tanner\n”;
fwrite($fh, $stringData);
fclose($fh);
PHP Truncate


In PHP Truncating is most often used on files that contain data that will only be used for a short time, before needing to be replaced. These type of files are most often referred to as temporary files.

$myFile = “HelloFile.txt”;
$fh = fopen($myFile, ‘w’);
fclose($fh);
To erase all the data from our HelloFile.txt file we need to open the file for normal writing. All existing data within HelloFile will be lost.

 
 
 

What are PHP Functions ?

In PHP programming functions can be defined as bunch of statements or script that is used to perform a specific task. In simple words PHP function can also be defined as set of instructions to execute a certain task .

PHP Function Syntax

Function Syntax
function my_function_name(parameters)
{
//block of code
}

In PHP following parameters should be noted for creating functions as per above syntax


-All functions start with the keyword “function()”
-Function should have names – It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number)
-Add a “{“- The function code starts after the opening curly brace
Insert the function code

-Add a “}” – The function is finished by a closing curly brace
Example of PHP Function


<html>
<body>

<?php
//we create a function name my_function
function my_function()
{
echo “Hello! How are you?”;
}
//we call our function like this when we want to use it
my_function();

?>

</body>
</html>
In the above code, we have created my_function() and then function is call by typing the its name. The above code outputs “Hello! How are you?”.

The above code outputs “Hello! How are you?”.

 
 
 

PHP Loops : How Looping statements are Defined in PHP ?

In terms of Programming, certain part of code or block of statements are executed at given number of times (repetitively ), depending upon particular condition to be checked against specific value .
The task become simple when looping statements are introduced to perform such operations. In simple terms looping statements are used to perform same type of execution or operations at regular intervals.

Looping statements are also known as control flow statements (for controlling the flow of programs in form of loops)

PHP supports following types of looping statements :


While

do..while

for
The while Loop
The while statement will execute a block of code if and as long a condition is true.
Syntax

while <condition>
block of statement to be executed

Example
The following example demonstrates a loop that will continue to execute as long as the variable i is less than, or equal to 5. i will increase by 1 each time the loop runs:
<html>
<body><?php
$i=1;
while($i<=5)
{
echo “The number is ” . $i . “<br />”;
$i++;
}
?></body>
</html>
The do…while Loop
The do…while statement will execute a block of statement at least once – it then will repeat the loop as long as a condition is true.

Syntax
do
{
block of statement to be executed ;
}
while <condition>;
Example

The following example will increment the value of i at least once, and it will continue incrementing the variable i while it has a value of less than 5:
<html>
<body><?php
$i=0;
do
{
$i++;
echo “The number is ” . $i . “<br />”;
}
while ($i<5);
?></body>
</html>
The for Loop

The for statement is used when you know how many times you want to execute a statement or a list of statements.
Syntax

for <initialization; condition; increment>
{
code to be executed;
}
The following example prints the text “Hello World!” five times:
<html>
<body><?php
for ($i=1; $i<=5; $i++)
{
echo “Hello World!<br />”;
}
?></body>
</html>
In the first statement, we initialize a counter variable to an number value.
In the second statement, we set a condition (a max/min number value) until the counter is reached.
In the third statement, we set a value by how much we want the counter variable to incremented .

If more than one variable is included in either the initialization or the increment section, then they should be separated by commas. The condition must evaluate to true or false.