0% found this document useful (0 votes)
20 views12 pages

PHP Programs Sort Oops STRH

Uploaded by

mi0606130
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views12 pages

PHP Programs Sort Oops STRH

Uploaded by

mi0606130
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PHP PROGRAMS DISCUSSED IN CLASS TODAY

PHP - Sort Functions For Arrays


sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the
value
krsort() - sort associative arrays in descending order, according to the key

//SORT
<html>
<body>

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "<br> Array Elements are: <br>";
print_r($cars);
echo "<br/> elements using vardump <br/>";
var_dump($cars);
//echo "<br>" .$cars;
sort($cars);
echo "<br> Printing the sorted array elements using index..";
echo "<br>";
echo $cars[0];
echo "<br>";
echo $cars[1];
echo "<br>";
echo $cars[2];
echo "<br>";
echo "Printing the sorted array elements using for loop..";
echo "<br>";
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>

<?php
$numbers = array(4, 6, 2, 22, 11);
echo "<br> Array Elements are using print r: <br>";
print_r($numbers);
echo "<br/> elements using vardump <br/>";
var_dump($numbers);
sort($numbers);
echo "<br> Printing the sorted array elements using index..";
echo "<br>";
echo $numbers[0];
echo "<br>";
echo $numbers[1]; echo "<br>";
echo $numbers[2]; echo "<br>";
echo $numbers[3]; echo "<br>";
echo $numbers[4]; echo "<br>";
echo "Printing the sorted array elements using for loop..";
echo "<br>";
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
</body>
</html>
//RSORT
<html>
<body>

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "<br> Array Elements are: <br>";
print_r($cars);
rsort($cars);
echo "<br>Printing the sorted array elements using for loop..";
echo "<br>";
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
<?php
$numbers = array(4, 6, 2, 22, 11);
echo "<br> Array Elements are: <br>";
print_r($numbers);
rsort($numbers);
echo "<br>Printing the sorted array elements using for loop..";
echo "<br>";
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
</body>
</html>
//KSORT
<html>
<body>

<?php
$age = array("krishna"=>"85", "Krishna"=>"37", "shiva"=>"55");
echo "Array elements are : <br/>";
print_r($age);
echo "<br> Sorting based on key using ksort <br>";
ksort($age);

foreach($age as $x => $x_value) {


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

</body>
</html>

//ASORT
<html>
<body>

<?php
$age = array("krishna"=>"85", "rama"=>"37", "shiva"=>"55");
echo "Array elements are : <br/>";
print_r($age);
echo "<br> Sorting based on value using asort <br>";
asort($age);

foreach($age as $x => $x_value) {


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

</body>
</html>
//ARSORT
<html>
<body>

<?php
$age = array("krishna"=>"85", "rama"=>"37", "shiva"=>"55");
echo "Array elements are : <br/>";
print_r($age);
echo "<br> Sorting based on value using arsort <br>";
arsort($age);

foreach($age as $x => $x_value) {


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

</body>
</html>

//KRSORT
<html>
<body>

<?php
$age = array("krishna"=>"85", "rama"=>"37", "shiva"=>"55");
echo "Array elements are : <br/>";
print_r($age);
echo "<br> Sorting based on value using krsort <br>";
krsort($age);

foreach($age as $x => $x_value) {


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

</body>
</html>
//USORT & URSORT
/*
Sort the elements of an array by values using a user-defined comparison
function
*/
<html>
<body>

<?php
function my_sort($a, $b) {
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
}

$a = array(4, 2, 8, 6);

usort($a,"my_sort");

foreach($a as $key => $value) {


echo "[" . $key . "] => " . $value;
echo "<br>";
}
?>

</body>
</html>

/*
// Sort the elements of an array by keys using a user-defined comparison
function:
<html>
<body>

<?php
function my_sort1($a, $b) {
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
}

$arr = array("a"=>4, "b"=>2, "c"=>8, "d"=>6);


uksort($arr, "my_sort1");

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


echo "[" . $key . "] => " . $value;
echo "<br>";
}
?>

</body>
</html>
*/
// STRING HANDLING FUNCTIONS
<html>
<body>
<?php
$n=Array("hi");
$a="abcd";
$b="hijk";
$c='bharat';
$d="mom dad";
echo"<h2>STRING HANDLING FUNCTIONS</h2><br/>";
echo"1.UPPER CASE<br/>";
echo strtoupper($c)."<br/>";
echo "2.lower case<br/>";
echo strtolower($c)."<br/>";
echo "3.upper case of first word<br/>";
echo ucfirst($c)."<br/>";
echo "4.lower case of first word<br/>";
echo lcfirst($c)."<br/>";
echo "5.upper case word<br/>";
echo ucwords($d)."<br/>";
echo "6.ltrim<br/>";
echo ltrim($b)."<br/>";
echo "7.rtrim<br/>";
echo rtrim($a)."<br/>";
echo "8.trim<br/>";
echo trim($b)."<br/>";
echo "9.print<br/>";
echo print($a)."<br/>";
echo"<br/>";
echo "10.string length<br/>";
echo strlen($c)."<br/>";
echo "11.string reverse<br/>";
echo strrev($c)."<br/>";
echo "12.chunk split<br/>";
echo chunk_split($c,1,'*')."<br/>";
echo "13.string compare<br/>";
echo strcmp($a,$b)."<br/>";
echo "14.string n compare<br/>";
echo strncmp("hello","hello",5)."<br/>";
echo "15.substring<br/>";
echo substr($c,2)."<br/>";
echo "16.substring count<br/>";
echo substr_count("hare krishna hare krishna","hare")."<br/>";
echo"17.substring compare<br/>";
echo substr_compare("hi","hello",0)."<br/>";
echo"18.substring replace<br/>";
echo substr_replace("women","ood",2)."<br/>";
echo"19.strstr<br/>";
echo strstr("helloworld","ello","iiii")."<br/>";
echo"20.string char<br/>";
echo strchr("hibharat","bharat")."<br/>";
echo"21.string position<br/>";
echo strpos("hello friends!","f")."<br/>";
echo"22.count chars<br/>";
echo count_chars($c,3)."<br/>";
echo"23.add slashes<br/>";
echo addslashes('hare "krishna" hare "rama"')."<br/>";
echo"24.strip slashes compare<br/>";
echo stripslashes("Hare \krishna")."<br/>";
echo"25.strchr() function searches for the first occurrence of a string
inside another string<br/>";
echo strchr("Hare Krishna Hare Rama","Krishna");
echo "<br/>Return the part of the string before the first occurence
<br/>";
echo strchr("Krishna Rama","Rama",true);
echo" <br/>26.The str_split() function splits a string into an array.<br/>";
print_r(str_split("Hare Krishna"));
echo "<br/>";
print_r(str_split("KriShiv",3));//Array ( [0] => Kri [1] => Shi [2] => v )
?>
</body>
</html>

//PHP CLASS OBJECT


<html>
<body>

<?php
class DCME { //class name
// Properties
public $name;
public $pin;

// Methods
//The $this keyword refers to the current object, and is // only available
inside methods.
function set_name($name)
{
$this->name = $name;
}
function get_name() {
return $this->name;
}

function set_pin($pin) {
$this->pin = $pin;
}
function get_pin() {
return $this->pin;
}
}
// creating objects to class
$shift1 = new DCME();
$shift2 = new DCME();
//assigning value to method
$shift1->set_name('Krishiv');
$shift1->set_pin('23001-CS-016');

$shift2->set_name('Hasini');
$shift2->set_pin('22001-CS-013');
// printing values
echo "name=".$shift1->get_name();
echo "<br>";
echo "pin=".$shift1->get_pin();
echo "<br>";
echo "name=".$shift2->get_name();
echo "<br>";
echo "pin=".$shift2->get_pin();
?>

</body>
</html>

//PHP CONSTRUCTORS
<html>
<body>

<?php
class Fruit {
public $name;
public $color;
/*If you create a __construct() function, PHP will automatically call this
function when you create an object from a class.

Notice that the construct function starts with two underscores (__)!
*/
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
function get_name() {
return $this->name;
}
function get_color() {
return $this->color;
}
}

$apple = new Fruit("Apple", "red");


echo $apple->get_name();
echo "<br>";
echo $apple->get_color();
?>

</body>
</html>

//PHP INHERITANCE
<html>
<body>

<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color)
{
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}

// Strawberry is inherited from Fruit


//class childclass extends baseclass
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}

$strawberry = new Strawberry("Strawberry", "red");


$strawberry->message();
$strawberry->intro();
?>

</body>
</html>

// OVER RIDING
<html>
<body>

<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}

class Strawberry extends Fruit {


public $weight;
public function __construct($name, $color, $weight) {
$this->name = $name;
$this->color = $color;
$this->weight = $weight;
}
public function intro() {
echo "The fruit is {$this->name}, the color is {$this->color}, and the
weight is {$this->weight} gram.";
}
}

$strawberry = new Strawberry("Strawberry", "red", 50);


$strawberry->intro();
?>

</body>
</html>

//PHP INTERFACE
<html>
<body>

<?php
// Interface definition
interface Father {
public function Relation();
}

// Class definitions
class Son implements Father {
public function Relation() {
echo " i am son <br>";
}
}

class Daughter implements Father {


public function Relation() {
echo " i am Daughter ";
}
}

// Create a list of Family


$son = new Son();
$daughter = new Daughter();

$family = array($son, $daughter);

// Tell the Relations


foreach($family as $rel) {
$rel->Relation();
}
?>
</body>
</html>

// multiple inheritance
<html>
<body>

<?php
// Interface definition
interface Father {
public function Relation();
}

interface Mother
{
public function display();
}
// Class definitions
class Son implements Father,Mother {
public function Relation() {
echo " i am son of father<br>";
}
public function display() {
echo " i am son of mother <br>";
}
}

class Daughter implements Father,Mother {


public function Relation() {
echo " i am Daughter of father <br> ";
}
public function display() {
echo " i am daughter of mother <br>";
}
}

// Create a list of Family


$son = new Son();
$daughter = new Daughter();

$family = array($son, $daughter);

// Tell the Relations


foreach($family as $rel) {
$rel->Relation();
$rel->display();
}
?>

</body>
</html>

You might also like