PHP Notes By Kshitij :: PART 6

PHP Library Functions

String Functions

 

Here Doc Operator For String And Methods

<<< Heredoc Operator: used for storing long string.

<?php

 

$msg=<<<abc

Hello All ‘We         lcome’ to “iBirds” and See Its a “String”,

<br> ‘Single’ and “Double”;

Shastri Nagar,

Ajmer

abc;

echo $msg;

//exit;

//die(“Bye”);

?>

 

strlen(String): It returns the length of string

<?php

$msg = “iBirds College”;

echo strlen($msg);

?>

 

substr(msg) : it returns number of character from string (: Returns a substring)

Syntax:

substr(string, start_from[, number_of_characters])

<?

$msg=”This is example”;

echo substr($msg,6);  //it will return string from 6 position i.e.   s example

echo substr($msg,8,4); //it will return  4 characters from 8 position i.e.  exam

?>

 

strtoupper(string)

<?

$msg = “hello india”;

echo strtoupper($msg);

?>

 

 

 

strtolower(string)

<?

$msg = “HELLO INDIA”;

echo strtolower($msg);

?>

 

ucfirst(string) : first letter upper case

<?

$msg = “hello world”;    //Hello world

echo ucfirst($msg);

?>

 

ucwords(string) : first letter of all words upper case

<?

$msg = “hello world hello india<br>”;  //Hello World Hello India

echo ucwords($msg);

$msg = “hello woRLd hElLO iNDIa”;

echo ucwords(strtolower($msg));

?>

 

strrev() : Reverses a string

 

<?php

echo strrev(“Hello World!”);

?>

 

str_repeat(string,timesOfRepeat)

 

<?

$msg =  “Welcome to iBirds<br>”;

echo str_repeat($msg,10);

?>

 

str_replace(CharacterToBeReplaced, NewCharacter, string)

<?php

//$msg =  “iBird Education”;

echo str_replace(“i”,”a”,$msg);

?>

 

 

ltrim(string), rtrim(String), trim(String)

 

<?php

$str=”       iBirds”;

echo “ABC”.ltrim($str);

?>

 

<?php

$str=”iBirds     “;

echo rtrim($str).”ABC”;

?>

 

<?php

$str=”       iBirds     “;

echo “ABC”.trim($str).”XYZ”;

?>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Array Functions

 

implode(saperator, array): Converts an array into String

 

<?php

$marks = array(3,4,4,5);

var_dump($marks);

$strMarks = implode(“,”,$marks);

echo “<br/>” . $strMarks . “<br/>”;

echo “<br/>”;

?>

 

explode(saperator, String) –> It converts a string into array

 

<?php

$msg = “Ajmer,Jaipur,Kota,Bikaner, 90”;

echo $msg;

$cities = explode(“,”,$msg);

echo $cities;

//Debugging Tools

//print_r($cities);  it will display array value with index number

//var_dump($cities);  it will print arrary value with index number and data type

foreach($cities as $value)

{

echo $value.”<br/>”;

}

?>

 

<?php

//count(array)  –> it returns size of array

$arr=array(“Name”=>”Raj”, “Age”=>15, “Marks”=>88.75, “Is_Married”=>true);

echo “Length of Array is => “.count($arr);

echo “<br/>”;

foreach($arr as $key=>$value)

{

echo $key.” = “.$value.”<br/>”;

}

echo “<br/>”;

?>

 

sort() – sort arrays in ascending order

<?php

//sort(array) ==> it sorts the array elements

//$marks = array(9,14,4,55,22,11);

$marks = array(“Mahendra”, “Gaurav”, “Akash”, “Saroj”, “Shipra”, “Bhawna”, “Priya”, “Raveena”, “Aman”);

sort($marks);

foreach($marks as $values)

{

echo $values.”<br/>”;

}

echo “<br/>”;

echo “<br/>”;

?>

 

rsort() – sort arrays in descending order

 

<?php

//$marks = array(9,14,4,55,22,11);

$marks = array(“Mahendra”, “Gaurav”, “Saroj”, “Shipra”, “Priya”, “Raveena”);

rsort($marks);

foreach($marks as $values)

{

echo $values.”<br/>”;

}

echo “<br/>”;

echo “<br/>”;

?>

 

asort() – sorts associative array in ascending order, according to the value

 

<?php

$age = array(“Peter”=>35, “Joe”=>43, “Ben”=>37, “AAA”=>12, “BBB”=>20, “CCC”=>5);

asort($age);

foreach($age as $key=>$value)

{

echo $key.” = “.$value.”<br/>”;

}

?>

 

arsort() – sorts associative array in descending order, according to the value

<?php

$age = array(“Peter”=>”35”, “Joe”=>”43”, “Ben”=>”37”, “CCC”=>”12”, “BBB”=>”20”, “AAA”=>”5″ );

arsort($age);

foreach($age as $key=>$value)

{

echo $key.” = “.$value.”<br/>”;

}

?>

 

ksort() – sorts associative array in ascending order, according to the key

 

<?php

$age = array(“Prakash”=>”35”, “Javed”=>”43”, “Badri”=>”37”, “Rohan”=>”12”, “Aman”=>”20”, “Waseem”=>”5″ );

ksort($age);

foreach($age as $key=>$value)

{

echo $key.” = “.$value.”<br/>”;

}

?>

 

 

krsort() – sorts associative arrays in descending order, according to the key

 

<?php

$age = array(“Devendra”=>”35”, “Aarti”=>”43”, “Vinod”=>”37”, “Kapil”=>”12”, “Shipra”=>”20”, “Raveena”=>”5″ );

krsort($age);

foreach($age as $key=>$value)

{

echo $key.” = “.$value.”<br/>”;

}

?>

 

 

 

 

is_array(array): Returns true (1) if argument is an array otherwise false (no value)

 

<?php

$arr=10;

//$arr = array(4,5,6,7);

echo is_array($arr) .”<br/>”;

var_dump($arr);

echo  “<br/>”;

 

$myArray = array(‘this’, ‘is’, ‘an’, ‘array’);

echo is_array($myArray) ? ‘This is an Array’ : ‘This is not an Array’;

?>

 

 

array_key_exists ==> Returns true (1) if finds key in an associative array otherwise false (no value).

 

<?php

$marks = array(“hindi” => 36, “english” => 34, “maths” => 67);

$res = array_key_exists(“hindi”,$marks);

//var_dump($marks);

echo “<br/>”;

var_dump($res);

echo  “<br/>”;

 

$res = array_key_exists(“Hindi”,$marks);

var_dump($res);

echo  “<br/>”;

?>

 

array_keys(array): Returns keys (create single dimension index array of keys) of an associative array

 

<?php

$marks = array(“hindi” => 36, “english” => 34, “maths” => 67);

$keys = array_keys($marks);

//echo $keys;

var_dump($keys);

echo  “<br/>”;

?>

array_values(array): Returns values of an associative array

 

<?

$values = array_values($marks);

var_dump($values);

echo  “<br/>”;

?>

 

 

 

isset($argument): It returns true if found value (in variable or array) otherwise false.

 

<?php

$a=1;

echo isset($a)?”Value is there”:”variable is empty”;

 

$marks = array(“hindi” => 36, “english” => 34, “maths” => 67);

//var_dump($marks);

 

$res = isset($marks[“hindi”]);

echo “<br/>”;

var_dump($res);

echo  “<br/>”;

$res = isset($marks[“Hindi”]);

var_dump($res);

echo  “<br/>”;

$res = isset($msg);

var_dump($res);

echo  “<br/>”;

$res = isset($msg_var);

var_dump($res);

echo  “<br/>”;

?>

in_array($argument): It returns true (1) if found value (in index array) otherwise false.<br/>

 

<?

$arr =array(“ajmer”,”kota”,”jaipur”);

echo in_array(“jaipur”,$arr);

?>

Date & Time Functions

<?php

echo date(“d-m-Y”).”<br/>”;  //31-07-2015

echo date(“d-M-y”).”<br/>”;  //31-Jul-15

echo date(“D d-M-y”).”<br/>”;  //Fri 31-Jul-15

echo date(“D d-M-Y”).”<br/>”;        //Fri 31-Jul-2015

 

//Change default Time Zone

//date_default_timezone_set(“Asia/Calcutta”);

 

date_default_timezone_set(‘America/Los_Angeles’);

 

$scr = date_default_timezone_get();

echo $scr;

echo “<br/><br/>”;

?>

<?php

//Date and Time

echo date(“d-m-Y h:i:s”) . “<br/>”;  //it willl display time as per given time zone

//Date and Time with am/pm

echo date(“d-m-Y h:i:s a”) . “<br/>”;

 

echo date(“M-Y”) . “<Br/>”;

?>

 

 

<?php

$msg = “Hello”;

printf(“%s”,$msg);

$num = 329.3883;

printf(“<br/>%.2f”,$num);

printf(“<br/>%c”,65);

printf(“<br/>%d”,78).”<br/>”;

for($i=65; $i<=90; $i++)

{

printf(“%c”,$i);

echo “<br/>”;

}

?>

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *