Ticker

6/recent/ticker-posts

PHP (Array)

PHP ARRAY
Array is a data structure that is used to store similar types of values in a single variable and in PHP we can create any array with help of a predefined keyboard "array". 

For example if you want to store roll number of 100 students then instead of defining 100 variables it’s easy to define an array with 100 lengths.

Let's have an example of array
$a=array[100];

Explanation: in the above program "$a" is an array variable, "array" is a keyword that is used to define an array in PHP and "100" its maximum size.


CHARACTERISTICS OF ARRAY
1. In any array values store with the help of index number and first value always store in zero index. 
For example in "a[4]" array capable to store maximum 4 values and its index number define as following representation. 
a[0]=40
a[1]=60
a[2]=75
a[3]=70

2. In PHP array's value can print with their index number by using of 'print_r' command.

Lets take an example of print_r command
$a=array(7,5,6,9);
print_r($a);

Note: print_r function is used to print array's content with index number.

Program to print array's value without index number
<?php
$a=array(34,50,26,30,45);
for($i=0 $i<5; $i++)
{
echo"<br>".$a[$i];
}
?>

Program to find largest value in array
<?php
$a=array(25,63,50,40,47);
$L=0;
for($i=0; $i<5; $i++)
{
echo"<br>".$a[$i];
if($L < $a[$i])
$L=$a[$i];
}
echo"<br>Largest value is:".$L;
?>





ARRAY BUILT FUNCTIONS
count: This function is used to count total value of selected array
Example
<?php
$a=array(25,63,50,40,47);

echo"Total value of array:".count($a);
?>

array_sum: This function is used to calculate sum of selected array's all values.
Example
<?php
$a=array(25,63,50,40,47);
for($i=0; $i<5; $i++)
{
echo"<br>".$a[$i];
}
echo"<br>Sum of array values: ".array_sum($a);
?>

sort: This function is used to arrange selected array's value in ascending order.
Example
<?php
$a=array(25,63,50,40,47);
echo"Before sorting<br>";
print_r($a);

sort($a);
echo"<br>After sorting<br>";
print_r($a);
?>

rsort: This function is used to arrange selected array's value in descending order.
Example
<?php
$a=array(25,63,50,40,47);
echo"Before sorting<br>";
print_r($a);

rsort($a);
echo"<br>After sorting<br>";
print_r($a);
?>


array_merge: This function is used to combine two or more arrays in a new array.
Example
<?php
$a=array(8,5,6);
$b=array(6,3,3);
$c=array_merge($a,$b);
print_r($c);
?>

array_reverse: This function is used to reverse a selected array.
Example
<?php
$a=array(1,2,3,4,5);
print_r (array_reverse($a));
?>

ARRAY STRING FUNCTIONS IN PHP

Strlen: This function is used to find total length of a given string 
Example
<?php
$name="Mukesh kumar";
$c=strlen($name);
echo "Total number of characters:".$c;
?>

Strtolower: Converts all characters of a string into lower case.
Example
<?php
$name="Mukesh kumar";

echo strtolower($name);
?>

Strtoupper: Converts the characters of a string into upper case.
Example
<?php
$name="Mukesh kumar";
echo strtoupper($name);
?>

Ord: We can get the ASCII value of entered character.
Example
<?php
$char="A";
echo "ASCII value is:".ord($char);
?>

Chr: To get the character from ASCII value.
Example
<?php
$num="66";
echo "ASCII value is:".chr($num);
?>

Strcmp: This function is used to compare two strings and returns zero, If both are same else it return a non-zero value.
Example
<?php
$a="Computer";
$b="Computer";

$c=strcmp($a,$b);

if($c==0)
{
   echo "Both strings are same";
}
else
echo "String are different";
?>

Strchr: By using this function we can get the characters of a string form specified character.
Example
<?php
$a="Computer";
echo strchr($a,"ut");
?>

Strstr: Its same as strchr function.
Strpos: to get the position of a character in a string and by default counting start by zero position.
Example
<?php
$a="Computer";
echo strpos($a,"C");
?>

Current: This function is used to show the first element of an array.
Example
<?php
$a=array(13,2,12,4,5);
echo end($a);
?>

Str_replace: This function is used to replace string with new string.
Example
<?php
echo str_replace("world","Peter","Hello world!");
?>

Str_repeat: This function is used to repeat string with specified number of times.
Example
<?php
echo str_repeat("world",5);
?>


Post a Comment

0 Comments