0% found this document useful (0 votes)
6 views

Program Code3php

This document contains examples of PHP code demonstrating various programming concepts like string functions, math functions, inheritance, anonymous functions, parameterized functions, constructors. The code snippets show how to use these PHP features to implement different programming tasks.

Uploaded by

Nikita Borhade
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Program Code3php

This document contains examples of PHP code demonstrating various programming concepts like string functions, math functions, inheritance, anonymous functions, parameterized functions, constructors. The code snippets show how to use these PHP features to implement different programming tasks.

Uploaded by

Nikita Borhade
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Program Code:

1.Calculate length of string.

<?php
$Str = "A VanishPokharkar";
echo strlen($Str);
?>

2. Count the number of words in string without using string


functions.

<html>
<head>
<title> Count the number of word in string </title>
</head>
<body>
<?php
$string = "hello, world! this is a sample string";
$word=1;
for ($i =0; $i < strlen($string); $i++)
{
if ($string[$i] == ' ')
{
$word++;
}
}
echo "The number of words in the string is:".$word. "\n";
?>
</body>
</html>
Exercise:
Write a program to demonstrate PHP maths function.

<?php
$n1=100;
$n2=5;
$n3=min($n1,$n2);
echo "Min is : $n3"."<br><br>";
$n1=max($n1,$n3);
echo "Max is : $n1"."<br><br>";
$n4=sqrt($n1);
echo "sqrt is : $n4"."<br><br>";
$n1=pow($n2,$n4);
echo "pow is : $n1"."<br><br>";
echo "ceil : ".ceil($n1)."<br><br>";
echo "floor : ".floor($n2)."<br><br>";
echo "round : ".round($n3.$n1)."<br><br>";
echo "abs : ".abs($n4);
?>
Program Code:
Write a program to demonstrate anonymous function.

<?php
$arr = [10,3,70,21,54];
usort ($arr, function ($x , $y) {
return $x > $y;
});
foreach ($arr as $x){
echo $x . "<br>";
}
?>
Exercise:
Write a Program to demonstrate parameterized function.

<?php
function addfunc($num1,$num2)
{
$num=$num1+$num2;
echo "sum of the two numbers is:".$num;
}
addfunc(50,20);
?>
Program Code:
1.Write a program to implement multilevel inheritance.

<?php
class grandparent
{
function dis1()
{
echo "Grand-Parent";
echo "<br>";

}
}
class parents extends grandparent
{
function dis2()
{
echo "Parents";
echo "<br>";
}
}
class child extends parents
{
function dis3()
{
echo "Child";
echo "<br>";
}
}
$obj=new child();
$obj->dis1();
$obj->dis2();
$obj->dis3();
?>
2.Write a program to implement multiple inheritance.
<?php
class Geeks
{
public function sayhello()
{
echo "Hello";
echo "<br>";
}
}
trait forGeeks
{
public function sayfor()
{
echo " Geeks";
echo "<br>";
}
}
class Sample extends Geeks
{
use forGeeks;
public function geeksforgeeks()
{
echo "GeeksforGeeks";
echo "<br>";
}
}

$test = new Sample();


$test->sayhello();
$test->sayfor();
$test->geeksforgeeks();
?>
Exercise:

1.Write a program to demonstrate parameterized constructor.

class Example

public $Name, $Age;

public function __construct($UserName, $UserAge)

$this->Name = $UserName;

$this->Age = $UserAge;

echo "<br>";

$Obj1 = new Example("Tom", 22);

$Obj2 = new Example("Dick", 26);

$Obj3 = new Example("Harry", 28);

print_r([$Obj1, $Obj2, $Obj3]);

?>

You might also like