IN PHP IN PERL
8. i) arguments passed by value: when an arguments pass by value any change for the value
inside the function owned be reflected in the variable outside the function
ii) Arguments passed by reference: - when an argument pass by resource and change for the
inside with be refer to the variable outside the function
Ex: -
<?php
function test($x,&$y){ $x +=2;
echo 'The Value of $x = ',$x,'<br>'; echo 'The Value of $y = ',$y,'<br>'; }
$a = 5; $b = 10;
echo '$a = ',$a,'<br>';//5 echo '$b = ',$b,'<br>';//10 test($a,$b);
echo '$a = ',$a,'<br>';//5 echo '$b = ',$b,'<br>';//10 ?> Built-in functions: - Math functions: - Ex: - <?php
echo 'Math Pi Value = ',M_PI,'<br>'; echo 'Math Pi Value = ',M_PI_2,'<br>'; echo 'Math Pi Value = ',M_PI_4,'<br>'; echo 'Eulers Constant = ',M_E,'<br>'; echo 'SQRT 2 = ',M_SQRT2,'<br>'; echo 'SQRT 2 = ',M_SQRT1_2,'<br>'; echo 'Log of 2 = ',log(2),'<br>';
echo 'Log base 10 of 2 = ',log10(2),'<br>'; echo 'Absolute Value = ',abs(-9),'<br>'; echo 'Exponent of 1 = ',exp(2),'<br>'; echo 'pi() = ',pi(),'<br>';
echo 'ceil value = ',ceil(2.1111),'<br>'; echo 'Floor Value = ',floor(2.9999),'<br>'; echo 'round = ',round(2.51458),'<br>'; echo 'round = ',round(2.51558,2),'<br>';
echo 'rad2deg(M_PI) = ',rad2deg(M_PI),'<br>'; echo 'rad2deg(M_PI) = ',rad2deg(M_PI_2),'<br>'; echo 'rad2deg(M_PI) = ',rad2deg(M_PI_4),'<br>'; echo 'deg2rad = ',deg2rad(180),'<br>';
echo 'sin 90 = ',sin(M_PI_2),'<br>'; echo 'tan 45 = ',tan(M_PI_4),'<br>'; echo 'hypot value = ',hypot(3,4),'<br>';
echo 'rand(min,max) = ',rand(100,1000),'<br>'; echo 'min value = ',min(10,20,30),'<br>'; echo 'max Value = ',max(2.999,2.998),'<br>'; ?>
Date & time functions: -
Date: - date is the pre-defined constant in php. It will generate current date according to the no
of arguments pass
Check date(): - let us known the validate in a year
j----> represents day of the month without leading zeros l---->view letter represents of the day of the way
L---> whether it’s a leap year (or) not
W---> week number of year weeks starts from Monday
w----> numerical represents of day of the way 0 for Monday through 6 for Saturday S---> English original safix for the day of the month two character ex: -st ,nd, rd,th F----> a full representation of the month such as January (or) March
M---> short representation of the month such as jan (or) mar
m---> numerical representation of the month with leading zero’s means 0-12 n---> numerical representation of month without zero’s which is 1-12
d---> number of days given in a month 28 (or) 31 y---> two digits representation of a year
Y---> it is representation of the full year
Time: -no of milliseconds sec right from unique time stamp
a---> lowercase ante meridiem and post meridiem A---> uppercase ante meridiem and post meridiem B---> swatch internet time
g---> 12- hours format of an hour without leading zeros G---> 24- hour format of an hour without leading zeros h---> 12- hours format of an hour with leading zeros H--->24- hours format of an hour with leading zeros
i---> minutes with leading zero’s s---> seconds with leading zero’s
u---> microseconds (added in php 5.2.2)
Ex: -
<?php
echo 'Time zone set to : ',@date_default_timezone_get(),'<br>'; date_default_timezone_set('America/Denver');
echo 'Time zone set to : ',@date_default_timezone_get(),'<br>'; echo 'Today Date : ',@date("M-dS-Y, h:i:s a"),'<br><hr>'; date_default_timezone_set('America/Los_Angeles');
echo 'Time zone set to : ',@date_default_timezone_get(),'<br>'; echo 'Today Date : ',@date("M-dS-Y, h:i:s a"),'<br><hr>'; date_default_timezone_set('Asia/Calcutta');
echo 'Time zone set to : ',@date_default_timezone_get(),'<br>'; echo 'Today Date : ',@date("M-dS-Y, h:i:s A"),'<br><hr>'; var_dump(checkdate(02,29,1975));
echo '<hr>No of milliseconds from Unix time Constant ',time(),'<br>'; echo '<hr>Feature Date & time <br>';
echo 'Event is on : ',(date("M-dS-Y, h:i:s a",mktime(7,9,52,8,23,2012))),'<br><hr>'; echo date('Y'); ?>
Array built-in function: -
$a = array(10,20,30); function r($arr,$func=''){ if(!empty($func)){ echo "<h2 align='center'><u>$func</u></h2>"; } foreach($arr as $k=>$v){ echo "$k = $v<br>"; } echo '<hr>'; }
echo 'Array count = ',count($a),'<br>'; echo 'Size of = ',sizeof($a),'<br>';
echo 'Sum of Array = ',array_sum($a),'<br>'; echo 'Product of Array = ',array_product($a),'<br>'; $a = array('Name'=>'Rajesh','Age'=>30,'Status'=>'Active','Email'=>'[email protected]','Gender'=>'Mal e'); r($a,'Normal Array'); $b = array_change_key_case($a,CASE_UPPER); r($b,'Upper Case'); $b = array_change_key_case($a,CASE_LOWER); r($b,'Lower Case'); $b = array_flip($a);
r($b,'Array Flip');
$b = array_change_key_case(array_flip($a),CASE_UPPER); r($b,'Array Flip Values');
$a = array('Doctor','Fruit','Software'); $b = array('Patient','Orange','PHP'); r(array_combine($a,$b),'Array Combine'); $a = array('Name'=>'Rajesh','Age'=>30,'Status'=>'Active','Email'=>'[email protected]','Gender'=>'Mal e');
echo '<h2 align="center">Array Chunk</h2>'; $b = array_chunk($a,3); echo '<pre>'; print_r($b); echo '</pre>'; $a = array(10,20,30); $b = array(10,20,40,50); r(array_merge($a,$b),'Array Merge'); r(array_intersect($a,$b),'Array Intersect'); r(array_diff($a,$b),'Array Diff'); $a = array('[email protected]','[email protected]','[email protected]','[email protected]','[email protected] om'); r(array_unique($a),'Array Unique'); r(array_reverse($a),'Array Reverse');
$a = array(10,20,30); array_unshift($a,5); r($a,'Unshift Method'); array_shift($a); r($a,'shift Method'); array_push($a,40); r($a,'Push Method'); array_pop($a); r($a,'Pop Method'); $ns = $ss = $as = $ks = array('Name'=>'Rajesh','Age'=>30,'Status'=>'Active','Email'=>'[email protected]','Gender'=>'Mal e'); r($ns,'Normal Array'); sort($ss); r($ss,'Sorted Array'); rsort($ss);
r($ss,'Reverse Sort Array'); asort($as);
r($as,'Associative Sort Array'); arsort($as);
r($as,'Associative Reverse Sort Array'); ksort($ks);
krsort($ks);
r($ks,'Key Sort Reverse Method');
echo "<h2 align='center'>array_key_exists , array_search , in_array</h2>"; $a =
array('Name'=>'Rajesh','Age'=>30,'Status'=>'Active','Email'=>'[email protected]','Gender'=>'Mal e');
echo 'Do i have Email Feild : ',var_dump(array_key_exists('Gender',$a)),'<br>'; echo 'Do we have Value with some : ',var_dump(array_search(30,$a)),'<br>'; echo 'Do we have Value with some : ',var_dump(in_array('Male',$a)),'<br>'; $a =
array('Name'=>'Rajesh','Age'=>30,'Status'=>'Active','Email'=>'[email protected]','Gender'=>'Mal e');
r(array_keys($a),'Array Keys Calling'); r(array_values($a),'Array Value Calling');
$str = 'Hey i am from India & my name is Praveen'; $b = explode(" ",$str);
//print_r($b);echo '<br>';
echo 'The Length of your total Words = ',count($b),'<br><hr>'; for($i=0;$i<count($b);$i++){
echo 'The index at ',$i,' = ',$b[$i],'<br>'; }
$a =
array('[email protected]','[email protected]','[email protected]','[email protected]','[email protected] m','[email protected]','[email protected]','[email protected]');
print_r($a);
$b = implode(',',$a); <br><br>
To : <input type='text' name='name' value='<?php echo $b;?>' size='60'/> <hr>
<?php
$a = array('born','child','teen','father','dead'); r($a,'Normal Array');
echo 'The Present Pointer is at : ',current($a),'<br>And the Key is at ',key($a),'<br>'; echo 'Next : ',next($a),'<br>';
echo 'The Present Pointer is at : ',current($a),'<br>And the Key is at ',key($a),'<br>'; echo 'Previous location : ',prev($a),'<br>';
echo 'The Present Pointer is at : ',current($a),'<br>And the Key is at ',key($a),'<br>'; echo 'End Value : ',end($a),'<br>';
echo 'The Present Pointer is at : ',current($a),'<br>And the Key is at ',key($a),'<br>'; echo 'The Present Pointer is at : ',current($a),'<br>And the Key is at ',key($a),'<br>'; echo 'Reset ',reset($a),'<br>';
echo 'The Present Pointer is at : ',current($a),'<br>And the Key is at ',key($a),'<hr>'; $a = array('Name'=>'Rajesh','Age'=>30);
r(Each($a),'Each Method'); r(Each($a),'Each Method');
list($gb,$intel,$dell) = $a;
echo "$gb & $intel together makes $dell faster<br><hr>"; $str = "write some large text matter.";
echo $str,'<br><hr>';
echo '<h2>Wordwrap</h2>'; echo wordwrap($str,20,'<br>',true); ?>
String builtin function: -
<?php echo '<h2>';
$str = "lamp institute";
echo "Normal String = ",$str,'<br>';
echo "Length of String = ",strlen($str),'<br>'; echo "To Upper Case = ",strtoUpper($str),'<br>'; echo "To Lower Case = ",strtoLower($str),'<br>'; echo "Upper case First Word = ",ucfirst($str),'<br>'; echo "Upper case words = ",ucwords($str),'<br>'; echo 'Reverse = ',strrev($str),'<br>';
echo '<hr>';
echo 'Position of str = ',strpos($str,'t'),'<br>'; echo 'last Position of str = ',strrpos($str,'t'),'<br>';
echo 'Position insensitive = ',stripos($str,'lamp'),'<br>'; echo 'last position at t insensitive = ',strripos($str,'T'),'<br>'; echo 'strstr will extract the entire string : ',strstr($str,"In"),'<br>'; echo 'strstr will extract the entire string : ',stristr($str,"in"),'<br>'; $name = 'Rajesh Kumar';
echo "substr = ",substr($name,0),'<br>'; echo "substr = ",substr($name,7,4),'<br>'; echo "substr = ",substr($name,-5,-1),'<br>'; $url = 'http://www.google.com?status=active'; $url .= '&';
echo 'substr = ',substr($url,0,-1),'<br>';
echo 'replace = ',str_ireplace('lamp','Apache',$str),'<br>';
$a = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; echo str_shuffle($a),'<hr>'; echo substr(str_shuffle($a),0,12); echo '<hr>'; $name = "O'henry"; echo $name,'<br>'; echo addslashes($name),'<br>'; echo stripslashes('O\'henry'),'<br>';
echo strip_tags("<b><i>Hi i am not feeling well</i></b>"); echo '<hr>';echo '<h2>';
$pwd = 'computer';
echo 'Original Name : ',$pwd,'<br>';
echo '<h1 align="center">One Way Encrypt</h1>';
echo 'Encrypt with MD5 = ',md5($pwd),'<br>';//32 char value
echo 'Encrypt with MD5 strict = ',md5($pwd,true),'<br>';//16 bit binary format echo 'Encrypt with sha1 = ',sha1($pwd),'<br>';//40 char vlaue
echo 'Encrypt with sha1 strict= ',sha1($pwd,true),'<br>';//20 binary vlaue echo 'Crypt = ',crypt($pwd),'<br>';
echo 'crypt salt = ',crypt($pwd,'d4'),'<br>';
echo '<h1 align="center">Encrypt & Decrypt Method</h1>'; echo 'encrypt = ',base64_encode($pwd),'<br>';
echo 'decrypt = ',base64_decode('Y29tcHV0ZXI='),'<br>'; echo '<h1 align="center">Url Encode & Decode</h1>'; $url = 'http://www.lamp.com?status=success&id=7&pid=20'; echo 'Original url = ',$url,'<br>';
echo 'encode = ',urlencode($url),'<br>';
echo 'decode = ',urldecode('http%3A%2F%2Fwww.lamp.com%3Fstatus%3Dsuccess%26id %3D7%26pid%3D20'),'<br><hr>';
echo '<pre>';
$name = " Rajesh ";
echo 'My name is :',$name,'<br>'; echo 'My name is :',rtrim($name),'<br>'; echo 'My name is :',ltrim($name),'<br>';
echo 'My name is :',trim($name),'<br>'; echo '</pre>';
Path: -
FILE:- The full path and filename of the file. If used inside an include, always contains an
absolute path with symlinks
LINE:-The current line number of the file. DIR:-The directory of the file.
Realpath:-Returns absolute pathname
Basename:-Returns filename component of path Ex: -
<?php
echo "File Information : ",__FILE__,'<br>'; echo "Base Name : ",basename(__FILE__),'<br>'; echo "Directory Name : ",dirname(__FILE__),'<br>'; echo "Directory Name : ",__dir__,'<br>';
echo "real Path : ",realpath(__FILE__),'<br>'; //echo "real Path : ",realpath('../..'),'<br>'; echo "Line NO : ",__LINE__,'<br>'; echo '<hr>';
$a = pathinfo(__FILE__); //print_r($a);
foreach($a as $k=>$v){ echo $k,' = ',$v,'<br>';
}
File-function: -