PHP Programs Sort Oops STRH
PHP Programs Sort Oops STRH
PHP Programs Sort Oops STRH
//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);
</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);
</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);
</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);
</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");
</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;
}
</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 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;
}
}
</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}.";
}
}
</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}.";
}
}
</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>";
}
}
// 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>";
}
}
</body>
</html>