0% found this document useful (0 votes)
12 views10 pages

OST LAB MANUAL

The document contains multiple PHP programs demonstrating various programming concepts such as functions, string manipulation, arrays, and object-oriented programming. It includes examples of user-defined and pre-defined functions, string functions, indexed and associative arrays, and class definitions with methods. Additionally, it covers introspection and serialization of objects in PHP.

Uploaded by

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

OST LAB MANUAL

The document contains multiple PHP programs demonstrating various programming concepts such as functions, string manipulation, arrays, and object-oriented programming. It includes examples of user-defined and pre-defined functions, string functions, indexed and associative arrays, and class definitions with methods. Additionally, it covers introspection and serialization of objects in PHP.

Uploaded by

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

7.

PASSING PARAMETER IN FUNCTION

PROGRAM

<html>

<head> </head>

<body>

<?php

function fact($n)

$f=1;

for($i=1;$i<$n;$i++)

$f=$f*$i;

return($f);

echo 'Factorial of given number';

echo '<br>';

echo '-----------------------------';

echo '<br>';

$n=5;

$k=fact($n);

echo "factorial of $n is $k";

?>

</body>

</html>

OUTPUT
8. write a php program using user-defined and pre-defined function

PROGRAM:

<?php

// User-defined function to calculate factorial

function calculateFactorial($num) {

if ($num < 0) {

return "Factorial is not defined for negative numbers.";

$factorial = 1;

for ($i = 1; $i <= $num; $i++) {

$factorial *= $i;

return $factorial;

// Using a pre-defined function: strlen()

$name = "PHP Programming";

$nameLength = strlen($name);

// Using another pre-defined function: date()

$currentDate = date("Y-m-d H:i:s");

// Using another pre-defined function: strtoupper()

$uppercaseName = strtoupper($name);

// Display results

echo "The factorial of 5 is: " . calculateFactorial(5) . "<br>";

echo "The length of the string '$name' is: $nameLength<br>";

echo "Current date and time: $currentDate<br>";

echo "Uppercase version of '$name': $uppercaseName<br>";


?>

9. write a php program using string

PROGRAM:

<?php

// Defining string variables

$greeting = "Hello";

$name = "John";

$sentence = "Welcome to PHP programming.";

// Concatenating strings using the dot (.) operator

$fullMessage = $greeting . ", " . $name . "! " . $sentence;

// Displaying the output

echo $fullMessage;

?>

OUTPUT:

Hello, John! Welcome to PHP programming.


10. PHP string functions.

PROGRAM:

<?php

// Define a string

$text = "Hello, PHP Programming!";

// Using strlen() to get the length of the string

$textLength = strlen($text);

// Using strrev() to reverse the string

$reversedText = strrev($text);

// Using strtoupper() to convert to uppercase

$uppercaseText = strtoupper($text);

// Using strtolower() to convert to lowercase

$lowercaseText = strtolower($text);

// Using str_replace() to replace a word

$replacedText = str_replace("PHP", "Web", $text);

// Using substr() to extract a part of the string

$substringText = substr($text, 7, 3);

// Displaying results

echo "Original Text: $text <br>";

echo "Length of String: $textLength <br>";

echo "Reversed Text: $reversedText <br>";

echo "Uppercase Text: $uppercaseText <br>";


echo "Lowercase Text: $lowercaseText <br>";

echo "Replaced Text: $replacedText <br>";

echo "Extracted Substring: $substringText <br>";

?>

11. write a php program using indexed array,assosiative array and multi dimensional array

PROGRAM:

<?php

// Indexed Array

$fruits = array("Apple", "Banana", "Cherry");

// Display Indexed Array Elements

echo "Indexed Array Elements:<br>";

echo $fruits[0] . "<br>"; // Apple

echo $fruits[1] . "<br>"; // Banana

echo $fruits[2] . "<br><br>"; // Cherry

// Associative Array

$student = array(

"name" => "John",

"age" => 20,

"course" => "Computer Science"

);

// Display Associative Array Elements


echo "Associative Array Elements:<br>";

echo "Name: " . $student["name"] . "<br>";

echo "Age: " . $student["age"] . "<br>";

echo "Course: " . $student["course"] . "<br><br>";

// Multidimensional Array

$students = array(

array("Alice", 21, "Mathematics"),

array("Bob", 22, "Physics"),

array("Charlie", 20, "Chemistry")

);

// Display Multidimensional Array Elements

echo "Multidimensional Array Elements:<br>";

for ($i = 0; $i < count($students); $i++) {

echo "Student " . ($i + 1) . ": ";

echo "Name: " . $students[$i][0] . ", ";

echo "Age: " . $students[$i][1] . ", ";

echo "Course: " . $students[$i][2] . "<br>";

?>

OUTPUT:

Indexed Array Elements:

Apple

Banana

Cherry

Associative Array Elements:


Name: John

Age: 20

Course: Computer Science

Multidimensional Array Elements:

Student 1: Name: Alice, Age: 21, Course: Mathematics

Student 2: Name: Bob, Age: 22, Course: Physics

Student 3: Name: Charlie, Age: 20, Course: Chemistry

11. write a php program using objects

PROGRAM:

<?php

// Define a class

class Car {

public $brand;

public $color;

// Method to set car details

public function setDetails($brand, $color) {

$this->brand = $brand;

$this->color = $color;

// Method to display car details

public function getDetails() {

echo "Car Brand: " . $this->brand . "<br>";


echo "Car Color: " . $this->color . "<br>";

// Create an object of the Car class

$car1 = new Car();

$car1->setDetails("Toyota", "Red");

// Display car details

$car1->getDetails();

?>

OUTPUT:

Car Brand: Toyota

Car Color: Red


12. PHP Program with Introspection & Serialization

PROGRAM:

<?php

// Define a class

class Person {

public $name;

public $age;

// Constructor

public function __construct($name, $age) {

$this->name = $name;

$this->age = $age;

// Method to display details

public function display() {

echo "Name: " . $this->name . "<br>";

echo "Age: " . $this->age . "<br>";

// Create an object

$person1 = new Person("John Doe", 30);

// **Introspection: Getting class details at runtime**

echo "<b>Introspection:</b><br>";

echo "Class Name: " . get_class($person1) . "<br>";


echo "Properties: ";

print_r(get_object_vars($person1));

echo "<br>Methods: ";

print_r(get_class_methods($person1));

echo "<br><br>";

// **Serialization: Converting object to string format**

$serializedData = serialize($person1);

echo "<b>Serialized Data:</b> " . $serializedData . "<br><br>";

// **Unserialization: Converting back to an object**

$unserializedPerson = unserialize($serializedData);

echo "<b>Unserialized Object Details:</b><br>";

$unserializedPerson->display();

?>

OUTPUT:

Introspection:

Class Name: Person

Properties: Array ( [name] => John Doe [age] => 30 )

Methods: Array ( [0] => __construct [1] => display )

Serialized Data: O:6:"Person":2:{s:4:"name";s:8:"John Doe";s:3:"age";i:30;}

Unserialized Object Details:

Name: John Doe

Age: 30

You might also like