Monday, 23 November 2015

phpFile Managment

php File Managment:

 Alright i am back after a long time so ... my new topic from today file management , no doubt its play an important role for any programming language its also important for php as well.
so i am starting from basic for file management operations.

we need to open a file,read a file ,do some modifications in files, i will discuss today how to read files in php.

Example:


<?php

// read files from my destination
$file = 'destination/phpguidefile.txt' or die('file not Found');
// to open file we use fopen
$fh = fopen($file, 'r') or die('file unable to open');
// to read file data/text use fread
$content = fread($fh, filesize($file)) or die('Reading Error');
// also its important to close file use fclose
fclose($fh);
// Echo text/data from file in php script
echo $content;

?>

Saturday, 31 October 2015

Read All Files From Directory

If we want to read all files name or specific files name from directory , no need of worry php provide B.I.F(built in function) name as glob().

What this method do?
glob search path name with provide patterns and fetch all related files.

glob('dirname'/*.*');
above method will fetch all files with any extensions we can also fetch particular extensions using below syntax
glob('*.ext');

Friday, 30 October 2015

Define Keyword Means


Define as name its clear that to declare some thing what is that thing its an variable? no its work as a constant once a define keyword use with any word it will never change or overwrite in php

define Syntax is 
define('word','value');
data type contains (Boolean,Integers,Float& Strings).

Example:


<?php
//print my constant
define('mycontnt','php-guides');

echo mycontnt;
//print output php-guides


?>

Tuesday, 21 July 2015

.htacess

.htacess:

 its a file which define who can access web page content and who cannot from all over internet or intranet.

Example:

if i need that use not access directory so we can write following text in htacess file
option-Indexes

Saturday, 7 February 2015

Function Return Values

In previous examples we just print the value from functions ,but if we need to do other tasks like need some results of calculations or some statements ,for this type of scenario in php we use return values in function Example:

<?php
// define a function for calculation
function calculate($val1,$val2,$opr) { 
   
if($opr=='add'){
return $val1+$val2;  // return value 
}
if($opr=='sub'){
return $val1-$val2;  // return value 
}
if($opr=='mul'){
return $val1*$val2;  // return value 
}
    

//call a function where first two parameters values and other mention operation you need to perform

$result = calculate(2,5,'mul');  //multiply operation

echo $result; 

//print output 10
?>


Function Passing Values by Refrence

As we discussed in previous e.g what is function passing by value ,in passing by value we can change value of variable by passing values , but if we want to change our global variable value than we use function by reference,in function by reference we pass parameters to function with the reference of variable not the value of variable as mention in below .

Example:

<?php
//declare gloabal variable 
$myvar= "global variable"; 

// function to show the value
function myfunction(&$myparameter) { 
    $changeparameter = "global vaiable inside"; 
    echo "This is $changeparameter  the function<br />"; 

// call function 
myfunction($myvar); 

// print global variable value
echo "This is $myvar outside the function";
 

?>

In above example (&) with parameter use to set reference for variable,it will modify global variable value


Friday, 6 February 2015

Function Parameter

As we see in precious example simple function example when we call that function its always do the same thing , what if we want to get dynamic values from functions? we use parameters with function,parameter also known as arguments.

Example:



<?php

// define custom function
function myfunction($myparameter) {
  
    echo $myparameter;

  
}
//call a function

myfunction("parameter1");

//each time its change value of function by pass argument
 
echo "<br/>";
myfunction("parameter2");
echo "<br/>";
myfunction("parameter3");
echo "<br/>";
myfunction("parameter4");
echo "<br/>";
myfunction("parameter5");
echo "<br/>";
//print output

parameter1
parameter2
parameter3
parameter4
parameter5
 ?>





This is also known as passing argument to function by value

Functions

As we know that php has its own built in function like other programming languages ,similarly in php we can build own functions, syntax for user defined function as below:



<?php
// define custom function function myfunction() {
   
       ...... // your code  }
//call a function
myfunction();
?>


Example:


<?php
/ define custom function
function myfunction() {
   
    echo"myfunction value";

   
}
//call a function

myfunction();

// print output myfunction value
?>



Monday, 2 February 2015

Remove Sessions

To remove or delete session we use two function destroy and unset first use unset to remove all sessions and then destroy  example:


 
<!DOCTYPE html>

<html>
<body>

<?php
session_start();


$_SESSION['mysession1']='php-guides1';
$_SESSION['mysession2']='php-guides2';
$_SESSION['mysession3']='php-guides3';
$_SESSION['mysession4']='php-guides4';
$_SESSION['mysession5']='php-guides5';
?>

<p style="color:red">Session Values=<?php 
//Remove Or delete Sessions
session_unset();

session_destroy();

echo "<pre style='color:red'>";
print_r($_SESSION);
echo "</pre>";



?>

</p>


</body>
</html>



//print output

Session Values=
Array
(
)