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

Roll No 042 PHP Assignment

php xml

Uploaded by

2022pgcsca042
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)
15 views

Roll No 042 PHP Assignment

php xml

Uploaded by

2022pgcsca042
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/ 9

1.Write a PHP program to print * symbol in alphabet pattern ' P '.

<?php

$rows = 7;

$cols = 6;

for($i = 0; $i < $rows; $i++){

for($j = 0; $j < $cols; $j++){

if(($j == 0 || $j == $cols - 1) || (($i == 0 || $i == $rows / 2) && $j < $cols - 1))

echo "*";

else

echo " ";

echo "<br>";

?>

output:

*****

* *

* *

*****

2. Write a PHP script that creates the following table (use for loops).

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20

3 6 9 12 15 18 21 24 27 30

4 8 12 16 20 24 28 32 36 40

5 10 15 20 25 30 35 40 45 50

6 12 18 24 30 36 42 48 54 60

7 14 21 28 35 42 49 56 63 70

8 16 24 32 40 48 56 64 72 80

9 18 27 36 45 54 63 72 81 90

10 20 30 40 50 60 70 80 90 100

<?php

// Define the number of rows and columns

$rows = 10;

$cols = 10;

// Iterate over each row

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

// Iterate over each column

for ($j = 1; $j <= $cols; $j++) {

// Calculate the value for the current cell

$value = $i * $j;

// Print the value followed by a tab

echo $value . "\t";

// Move to the next line after each row


echo "<br>";

?>

3. Write a PHP script to remove nonnumeric characters except comma and dot.

Sample string : '$123,34.00A'

Expected Output : 12,334.00

<?php

// Sample string

$sample_string = '$123,34.00A';

// Remove non-numeric characters except comma and dot

$cleaned_string = preg_replace("/[^0-9,.]/", "", $sample_string);

// Output the cleaned string

echo $cleaned_string;

?>

4. Write a PHP program to check which number nearest to the value 100 among two given
integers. Return 0

if the two numbers are equal.

Sample Input:

78, 95

95, 95

99, 70
Sample Output:

95

99

<?php

function nearest_to_100($num1, $num2) {

$target = 100;

// Calculate absolute differences between target and each number

$diff1 = abs($target - $num1);

$diff2 = abs($target - $num2);

// Check if both numbers are equal

if ($diff1 == $diff2) {

return 0; // Return 0 if numbers are equal

} elseif ($diff1 < $diff2) {

return $num1; // Return num1 if it's closer to the target

} else {

return $num2; // Return num2 if it's closer to the target

// Sample Input

$input = array(
array(78, 95),

array(95, 95),

array(99, 70)

);

// Iterate over each input pair

foreach ($input as $pair) {

// Call the function and output the result

echo nearest_to_100($pair[0], $pair[1]) . "\n";

?>

5. Write a PHP program to create a new array from a given array of integers shifting all zeros
to left direction.

Sample Input:

{ 1, 2, 0, 3, 5, 7, 0, 9, 11 }

Sample Output:

New array: 0,0,1,3,5,7,2,9,11

<?php

function shiftZerosToLeft($arr) {

$n = count($arr);

$result = array_fill(0, $n, 0); // Create an array of zeros with the same size as $arr

$index = 0; // Initialize index to insert non-zero elements

// Iterate through the array


for ($i = 0; $i < $n; $i++) {

// If the current element is not zero, insert it at the current index

if ($arr[$i] != 0) {

$result[$index++] = $arr[$i];

return $result;

// Sample input array

$input = [1, 2, 0, 3, 5, 7, 0, 9, 11];

// Call the function and get the new array

$newArray = shiftZerosToLeft($input);

// Output the result

echo "New array: " . implode(",", $newArray);

?>

6. Write a PHP class called 'Vehicle' with properties like 'brand', 'model', and 'year'.
Implement a method to

display the vehicle details.

<?php

class Vehicle {
// Properties

public $brand;

public $model;

public $year;

// Constructor

public function __construct($brand, $model, $year) {

$this->brand = $brand;

$this->model = $model;

$this->year = $year;

// Method to display vehicle details

public function displayDetails() {

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

echo "Model: " . $this->model . "<br>";

echo "Year: " . $this->year . "<br>";

// Example usage

$vehicle1 = new Vehicle("Toyota", "Corolla", 2020);

$vehicle1->displayDetails();

// You can create more instances and display their details as needed
?>

7. Write a PHP class called 'Student' with properties like 'name', 'age', and 'grade'. Implement
a method to

display student information.

<?php

class Student {

// Properties

public $name;

public $age;

public $grade;

// Constructor

public function __construct($name, $age, $grade) {

$this->name = $name;

$this->age = $age;

$this->grade = $grade;

// Method to display student information

public function displayInformation() {

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

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

echo "Grade: " . $this->grade . "<br>";

}
// Example usage

$student1 = new Student("John Doe", 18, "12th");

$student1->displayInformation();

// You can create more instances and display their information as needed

?>

You might also like