> February 2013 ~ Online tutorial

argument passed by reference

By default arguments are passed by value(so that it will changed inside function only not outside the function).So we are going the value then send value by reference,prepend by (&) to the argument name in the function definition.
Syntax
function fun_name(&$varname)
Example

<?php
function my(&$num)
{
$num='welcome';
}
$str="welcome";
my($str);
echo $str;

?>


Output


passing arguments

Information may be passed to function via the argument list.which is a comma-delimited list of expression
Example

<?php
function my($num)
{
echo "$num";
}
my("india");
?>


Output

conditional operator

The ? operator is equivalent to an if statement.it os called a terary operator because it takes three parameters an expression :an expression that is evaluated to be TRUE and FALSE an expression that is evaluates if it tree and expression evaluated if the first is FALSE

Syntax
(expr1)?(expr2):(expr3);
Example

?php
$n=6;
$b=7;
$val=($num 1>b)?$n:$b;
echo "The greatest value is $val";

?
output

require_once in php

The require_once()statement include and evaluates the specified file during of execution of the script.This is a behavior similar to the require() function with the only difference being that it code from a file has already been included.It will not be included again.

Syntax
include "function name";
Example
vars.php


<?php
$color='red';
$fruit='apple';
echo $fruit."color is".$color"
?>
test.php

<?php
echo "a $color $fruit"
// include 'vars.php
require_once'vars.php'
echo "a $color $fruit"
?>
Output:



require in php

The require() function is identical to include() except that is handle error differently.
The the function include() generates a warning(but the script continue execution) while the require() function generates a fatal error (and script stop)
Syntax
include "function name";
Example
vars.php


<?php
$color='red';
$fruit='apple';
echo $fruit."color is".$color"
?>
test.php

<?php
echo "a $color $fruit"
// include 'vars.php
require'vars.php'
echo "a $color $fruit"
?>
Output:



include_once in php

The include_once()statement include and evaluates the specified file during of execution of the script.This is a behavior similar to the include() function with the only difference being that it code from a file has already been included.It will not be included again.

Syntax
include "function name";
Example
vars.php


<?php
$color='red';
$fruit='apple';
echo $fruit."color is".$color"
?>
test.php

<?php
echo "a $color $fruit"
// include 'vars.php
include_once'vars.php'
echo "a $color $fruit"
?>
Output:

include in php

The include() function takes all the text in a specified file and copies it into file that used the include function
Syntax
include "function name";
Example
vars.php


<?php
$color='red';
$fruit='apple';
echo $fruit."color is".$color"
?>
test.php

<?php
echo "a $color $fruit"
// include 'vars.php
include 'vars.php'
echo "a $color $fruit"
?>
Output:



echo and print

The echo() and print() function are used to output the given argument.it can output all types single and multiple outputs can be made with these command
Syntax
int print(string $arg)
void echo (string $arg1[,string $ ,,])
Similarities
They both language constructs (not function)so they can be used without parentheses
Example


<?php
echo "welcome to india":
print "welcome to india";

?>

Output:

PHP directory

PHP file

PHP File
PHP has powerful file handling function to manage file in a server.Some of these function require some special setting in php.ini and some available by default.

Function in File
write file
read file
open the file

localtime in php

The localtime() function returns the array that contains the time components of unix time system
Syntax
localtime(timestamp,is_associative)
Example


<?php
print_r(localtime());
echo("

");
print_r(localtime(time(),true));
?>
output


idate in php

idate() function formats a local time or date as integer
Syntax
idate(format,timestamp)
Example


<?php
echo(idate("Y")):
?>
Output:


time in php

time() function returns the current time as unix tiemstamp (the number of seconds since jan 1 1970 00:00:00 GMT)
Syntax
time(void)
Example


<?php
$curr=time();
print_r("$curr");
?>
Output:

getdate() in php

getdate() function returns an array that contains date and time information for unix system
the returning array contain ten elements with relevant information needed when formatting a date

  • [seconds]-seconds
  • [minutes]-minutes
  • [hours]-hours
  • [mday]-day of month
  • [wday]-day of week
  • [year]-year
  • [yday]-day of year
  • [weekday]-name of the weekday
  • [month]-name of the month
Syntax
getdate(timestamp)
Program


<?php
$today=getdate(date("U"));
print("$today[weekday],today[month]],today[mday],today[year]");
?>

Output


date and time in php

date and time function allow you to extract and format the date and time on the server.
Note
These function depend on the locale settings of the server
date()

date() function formats a local time/date
The following describes the format arguments
  • a:am or pm
  • A:AM or PM
  • d:Numeric day of the month
  • D:Short day abbreviation
  • F:Full month name
  • g:12-hour time without leading zero
  • G:24-hour time without leading zero
  • h:12-hour time with leading zero
  • H:24-hour time without leading zero
  • i:mintues with leading zero
  • L:Boolean indicating a leap year
  • m:Numeric month with leading zero
  • M:Short month abbreviation
  • n:Numeric month without leading zero
  • O:GMT offset in[+]HHMM format
  • s:seconds
  • S.Suffix to numerical date
  • T:Time zone name
  • U:Unix seconds
  • w:Numeric day of the week
  • y:two digit year
  • Y:Four digit year

list() in php

list() function is used to assign values to a list of variable in one operation

syntax
list(var1,var2,...)


<?php

$sum=array("india","pak");
list($a,$b)=$sum;
echo "we are ,a $a,amd $b";


?>

Output

we are a india and pak

ksort() in php

ksort() function sorts an array by the keys.The values keep their original keys

syntax
ksort(array,sorttype)


<?php

$sum=array(a=>"india",b=>"pak");
ksort($sum);
print_r($sum);

?>

Output
Array(a=>india
b=>pak)

in_array() in php

in_array() function searched an array for a specific value.This function returns TRUE if the value is found in the array or FALSE otherwise.
syntax
in_array(search,array,type)


<?php

$sum=array("india","pak");
if(in_array("india",$sum)
{
echo"Match Found"
}
echo"Not found";
}

?>

Output
Match Found

count() in php

count() function counts the element of an array,of the properties or two properties of an array.
Syntax
count(array1,array2)
Example


<?php

$sum=array("india","pak");
// count the elements of an array
echo count($sum);

?>

Output
2

array_push() in php

array_push() function inserts one or more element to the end of an array
syntax
array_push(array,val1,val2,...)


<?php

$sum=array("india","pak");
array_push($arr,"usa","uk");

print_r($sum);

?>

Output
Array([0]=>india
[1]=>pak
[2]=>usa
[3]=>uk)

asort() in php

asort() function sorts an array by the values.The values keep their original keys.
syntax
asort(array,sorttype)


<?php
$sum=array(0=>"apple",1=>"orange",2=>"pine apple");
asort($sum);
print_r($sum);

?>

Output
Array([0]=>apple
[1]=>orange
[2]=>pine apple)

array_values() in php

The array_values() function returns the array containing all the values of an array

syntax
array_values(array)


<?php
$sum=array(0=>"apple",1=>"orange",2=>"pine apple");
// returns the array containing all the values of an array
print_r($array);

?>

Output
Array(0=>apple
1=>orange
2=>pine apple)

array_unique() in php

array_unique() function removes the duplicate values from an array.if two or more array values an the same ,the sirst apperence will be kept and the other will be removed
Syntax
arrray_unique(array)
Example

<?php
$sum=array(0=>"apple",1=>"orange",2=>"apple",);
echo "before using array unique";
print_r($sum);
echo "after using array unique";
print_r(array_unique($sum));

?>

Output
before using array unique

array(0=>apple
1=>orange
2=>apple)
after using array unique
array(0=>apple
1=>orange)

array_sum() in php

The array_sum() function returns the sum of all the values in the array

Syntax
array_sum(array)
Example

<?php
// sum the all the values in the array
$sum=array(0=>15,1=>15,1=>15,);
echo array_sum($sum);

?>

Output
45

trim() in php

trim() function remove the whitespaces and other predefined character from tht both side of a string

Syntax
trim(character)
Example

<?php
$str=" hello india";
echo "without trim".$str;
// remove whitespaces from the both the string
echo "with trim".trim($str);

?>

Output
without trom: hello india
with trim:hello india

substr

substr() function returns the part of string
Syntax
substr(string,start,length)
Example

<?php
// returns the part of string
echo substr("hello india",6);
echo substr("hello india",6,5);
?>

Output
india
india

strpos() in php

strpos() function returns the position of the first occurrence of string inside another string.
Syntax
strpos(string,find,start)
Example

<?php
// returns the position of the first occurrence of string
echo strpos("hello india","io");
?>




Output
6

strlen() in php

The strlen() function returns the length of string
Syntax
strlen(string)
example

<?php
// returns the length the string
echo strlen("hello india");
?>

Output
11

str_split() in php

The str_split() function splits the string into an array
Syntax
str_split(string,length)
Example

<?php
// split the string into an array
print_r(str_split("hello"));
print_r(str_split("hello",2));
?>

Output
Array([0]=>h
[1]=>e
[2]=>l
[3]=>l
[4]=>o
)
Array([0]=>he
[1]=>ll
[2]=>o)

strrev() in php

The strrev() function reverses the string
Syntax
strrev(string)
Example


<?php
echo strrev("one line");
?>

Output
enil eno

str_repeat() in php

The str_repeat() function repeats a string specified number of times.
Syntax
str_repeat(string,repeat)
Example

<?php
echo str_repeat("india",4);
?>

output
indiaindiaindiaindia

nl2br() in php

The nl2br() function inserts HTML line breaks(
)in front of each newline (\n)in a string
Syntax
nl2br(string)
Example

<?php
echo nl2br("one line .\n another line");
?>

Output

one line
another line

The html code look like this 

one line<br/>
another line

join() in php

join() function in php
The join()returns the string from the elements of an array.The join() function is an alias of the implode function
Syntax
join(separator,array)
Example


<?php
$str='hello india';
// it is breaks the string into an array
$exp_str=explode("",$str);
print_r($exp_str);
// it returns the string from the elements of an array
echo join("",$exp_str);
?>
Output:

Array([0]=>hello
[1]=>india)

hello india

implode() in php

The implode()function returns a string from the element of an array

Note
  • The implode() function accept its parameter in either order.How ever for consistency with explode().you should use the document order of argument .The separator parameter of imploded is optional.
Syntax:

implode(separator,array)

Example


<?php
$str='hello india';
// it is breaks the string into an array
$exp_str=explode("",$str);
print_r($exp_str);
// it returns the string from the elements of an array
echo implode("",$exp_str);
?>

Output:
Array([0]=>hello
[1]=>india)
hello india

Explode() in php

Explode()
The explode() functon breaks a string into an array
Note
Separator cannot be empty
Synatx
explode(separator,string,limit)
Example

<?php
$str="welcome to india";
echo explode("",$str);
?>
Output
Array([0]=>hello
[1]=>to
[2]=>india)

chunk_split() in php

chunk_split() in php
the chunk_split() function splits a string into a series of smaller parts
Note
This function does not alter the original string
syntax
chunk_split(string,length,end)
Example

<?php
$str="hello india";
// we will split the string after each character and add a"." after each split
echo chunk_split($str,1,'.');
?>

Output
h.e.l.l.o. .i.n.d.i.a.

chr() in php

chr()
The chr() function returns a character from the specified ASCII value
syntax
chr(ascii)
Example

<?php
// returns a character from the specified ASCII value
echo chr(52);
echo chr(052);
?>
Output
4
*

chop in php

chop()
The chop() function will remove a white space or other predefined character from the right end of string.This function is an alias the rtrim()function
Syntax
chop(string,charlist)
Example

<?php
$str="welcome to   ";
echo $str;
echo "india";
echo "<br>";
//remove the whitespace from the right end of a string
ech chop($str);
echo "india";
?>

Output
welcome to india
welcome toindia

addcslashes() and backcslashes() in php

addcslashes() and backcslashes()
The addcslashes() function return a string with backslashes in front of the specified character
Syntax
string addcslashes( string $str,string $charlsit)

Example

<?php
$str="welcome to india";
echo $str;
echo addcslashes($str,'c');
?>


Output:



wel\come to india

backcslashes()
The backcslashes() function removed the character added from addcslashes() function
Syntax:

backcslashes(string)

<?php
echo backcslashes("wel\come to india");
?>
Output
welcome to india